Skip to content
Snippets Groups Projects
Commit 4dc7459f authored by Oleksandr Tymoshchuk's avatar Oleksandr Tymoshchuk Committed by Ivan Doroshenko
Browse files

Issue #3353160 by o_tymoshchuk, Matroskeen: Add "Template" support to email sending

parent f9f13792
No related branches found
No related tags found
1 merge request!15Resolve #3353160 "Add to email sending template support"
......@@ -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]);
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment