Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
U
unione
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
unione
Commits
4dc7459f
Commit
4dc7459f
authored
1 year ago
by
Oleksandr Tymoshchuk
Committed by
Ivan Doroshenko
1 year ago
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#3353160
by o_tymoshchuk, Matroskeen: Add "Template" support to email sending
parent
f9f13792
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!15
Resolve #3353160 "Add to email sending template support"
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/Plugin/Mail/UniOneMail.php
+54
-2
54 additions, 2 deletions
src/Plugin/Mail/UniOneMail.php
with
54 additions
and
2 deletions
src/Plugin/Mail/UniOneMail.php
+
54
−
2
View file @
4dc7459f
...
...
@@ -124,7 +124,18 @@ class UniOneMail implements MailInterface, ContainerFactoryPluginInterface {
public
function
mail
(
array
$message
)
{
[
$body
,
$headers
]
=
$this
->
buildMessage
(
$message
);
$response
=
$this
->
client
->
emails
()
->
send
(
$body
,
$headers
);
try
{
$response
=
$this
->
client
->
emails
()
->
send
(
$body
,
$headers
);
}
catch
(
\Exception
$e
)
{
$this
->
logger
->
error
(
'Unable to send message from %from to %to: %code %message'
,
[
'%from'
=>
$message
[
'from'
],
'%to'
=>
$message
[
'to'
],
'%code'
=>
'Error code: '
.
$e
->
getCode
(),
'%message'
=>
$e
->
getMessage
(),
]);
}
if
(
!
empty
(
$response
[
'status'
])
&&
$response
[
'status'
]
===
'success'
)
{
// Debug mode: log all messages.
...
...
@@ -153,7 +164,7 @@ class UniOneMail implements MailInterface, ContainerFactoryPluginInterface {
]);
}
return
$response
[
'status'
]
===
'success'
;
return
isset
(
$response
)
&&
$response
[
'status'
]
===
'success'
;
}
/**
...
...
@@ -244,6 +255,22 @@ class UniOneMail implements MailInterface, ContainerFactoryPluginInterface {
unset
(
$message
[
'headers'
][
'Accept'
]);
}
// Support Unione templates.
if
(
!
empty
(
$message
[
'params'
][
'template_id'
]))
{
$unione_message
[
'template_id'
]
=
$message
[
'params'
][
'template_id'
];
// If template is set, we assume the default email body should be removed.
// List of template fields can be provided in 'template_fields' parameter.
if
(
!
isset
(
$message
[
'params'
][
'template_fields'
]))
{
$message
[
'params'
][
'template_fields'
]
=
[
'body'
];
}
// Remove email parameters to use the ones from template.
if
(
!
empty
(
$message
[
'params'
][
'template_fields'
]))
{
$this
->
applyTemplateFields
(
$unione_message
,
$message
[
'params'
][
'template_fields'
]);
}
}
// Make sure the files provided in the attachments array exist.
if
(
!
empty
(
$message
[
'params'
][
'attachments'
]))
{
$attachments
=
[];
...
...
@@ -379,4 +406,29 @@ class UniOneMail implements MailInterface, ContainerFactoryPluginInterface {
}
}
/**
* Unsets email parameters to apply the template values.
*
* By default, email fields have higher priority than template parameters.
* Therefore, if email has body, it'll be used instead of template body.
*
* This method removes some default email parameters to use template fields.
* A list of email parameters can be controlled by
* $message['params']['template_fields'] array item.
*
* @param array $message
* The Unione message array.
* @param array $template_parameters
* A list of parameters to be removed from the email array.
*/
public
function
applyTemplateFields
(
array
&
$message
,
array
$template_parameters
):
void
{
$keys
=
[
'body'
,
'from_email'
,
'from_name'
,
'subject'
];
foreach
(
$template_parameters
as
$item
)
{
if
(
in_array
(
$item
,
$keys
)
&&
!
empty
(
$message
[
$item
]))
{
unset
(
$message
[
$item
]);
}
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment