Commit 86e489ce authored by Leo's avatar Leo

Refactor EmailHelper

parent d39aa8d9
Pipeline #50428953 passed with stages
in 8 minutes and 28 seconds
......@@ -16,7 +16,6 @@ final class EmailHelper
* @var InfluxMetrics
*/
private $metrics;
private $emailTemplateAdminGateway;
private $sanitizerService;
private $twig;
......@@ -33,74 +32,133 @@ final class EmailHelper
}
private function emailBodyTpl(string $message, $email = false, $token = false): string
{
$unsubscribe = $this->getRenderedUnsubscribe($email, $token);
$message = $this->normalizeMessage($message);
return $this->twig->render('emailTemplates/body.html.twig', ['MESSAGE' => $message, 'UNSUBSCRIBE' => $unsubscribe]);
}
private function getRenderedUnsubscribe($email = false, $token = false)
{
$unsubscribe = $this->twig->render('emailTemplates/unsubscribe.html.twig', []);
if ($email !== false && $token !== false) {
$unsubscribe = $this->twig->render('emailTemplates/unsubscribe_newsletter.html.twig', array('TOKEN' => $token, 'EMAIL' => $email));
$unsubscribe = $this->twig->render('emailTemplates/unsubscribe_newsletter.html.twig', ['TOKEN' => $token, 'EMAIL' => $email]);
}
return $unsubscribe;
}
private function normalizeMessage(string $message)
{
$message = preg_replace('/(<[^>]+) style=".*?"/i', '$1', $message);
$search = array('<a', '<td', '<li');
$replace = array('<a style="color:#F36933"', '<td style="font-size:13px;font-family:Arial;color:#31210C;"', '<li style="margin-bottom:11px"');
$search = ['<a', '<td', '<li'];
$replace = ['<a style="color:#F36933"', '<td style="font-size:13px;font-family:Arial;color:#31210C;"', '<li style="margin-bottom:11px"'];
$message = str_replace($search, $replace, $message);
return $this->twig->render('emailTemplates/body.html.twig', array('MESSAGE' => $message, 'UNSUBSCRIBE' => $unsubscribe));
return $message;
}
public function tplMail($tpl_id, $to, $var = [], $from_email = false)
{
$mail = $this->composeMail($tpl_id, $to, $var, $from_email);
$mail->send();
$this->metrics->addPoint('outgoing_email', ['template' => $tpl_id], ['count' => 1]);
}
public function tplMail($tpl_id, $to, $var = array(), $from_email = false)
private function composeMail($tpl_id, $to, $var, $from_email)
{
$message = $this->composeMessage($tpl_id, $var);
$mail = new AsyncMail($this->mem);
$this->setEmailFrom($mail, $var, $from_email);
$htmlBody = $this->setAndReturnEmailHTMLBody($mail, $message);
$this->setEmailBody($mail, $htmlBody);
$this->setEmailSubject($mail, $message);
$this->setEmailTo($mail, $to);
return $mail;
}
private function setEmailFrom(AsyncMail $mail, $var, $from_email)
{
if ($from_email !== false && $this->validEmail($from_email)) {
$mail->setFrom($from_email);
} else {
$emailName = '';
if (array_key_exists('sender', $var)) {
$emailName = $var['sender'];
} elseif (array_key_exists('poster', $var)) {
$emailName = $var['poster'];
}
if (array_key_exists('bezirk', $var)) {
$emailName .= ' in ' . $var['bezirk'];
}
if ($emailName !== '') { // if sender information is present
$emailName .= ' via '; // though this is optional...
}
$emailName .= DEFAULT_EMAIL_NAME;
$emailName = $this->composeEmailName($var);
$mail->setFrom(DEFAULT_EMAIL, $emailName);
}
}
private function composeEmailName($var)
{
$emailName = '';
if (array_key_exists('sender', $var)) {
$emailName = $var['sender'];
} elseif (array_key_exists('poster', $var)) {
$emailName = $var['poster'];
}
if (array_key_exists('bezirk', $var)) {
$emailName .= ' in ' . $var['bezirk'];
}
if ($emailName !== '') { // if sender information is present
$emailName .= ' via '; // though this is optional...
}
$emailName .= DEFAULT_EMAIL_NAME;
return $emailName;
}
private function composeMessage($tpl_id, $var)
{
$locale = 'de-de';
$tpl_prefix = 'emailTemplates/' . $tpl_id . '.' . $locale;
$var = array_change_key_case($var, CASE_UPPER);
$message = array(
$message = [
'subject' => $this->twig->render($tpl_prefix . '.subject.twig', $var),
'body' => $this->twig->render($tpl_prefix . '.body.html.twig', $var)
);
'body' => $this->twig->render($tpl_prefix . '.body.html.twig', $var),
];
return $message;
}
private function setAndReturnEmailHTMLBody(AsyncMail $mail, $message)
{
$htmlBody = $this->emailBodyTpl($message['body']);
$mail->setHTMLBody($htmlBody);
// playintext body
return $htmlBody;
}
private function setEmailBody(AsyncMail $mail, $htmlBody)
{
$plainBody = $this->sanitizerService->htmlToPlain($htmlBody);
$mail->setBody($plainBody);
}
private function setEmailSubject(AsyncMail $mail, $message)
{
$subject = $message['subject'];
if (!$message['subject']) {
$message['subject'] = 'foodsharing-Mail: {EXCERPT}';
if (!$subject) {
$subject = 'foodsharing-Mail: {EXCERPT}';
}
if (mb_strpos($message['subject'], '{EXCERPT}') !== false) {
if (mb_strpos($subject, '{EXCERPT}') !== false) {
$plainMessage = $this->sanitizerService->htmlToPlain($message['body']);
$subjectLength = mb_strlen($message['subject']) - strlen('{EXCERPT}');
$subjectLength = mb_strlen($subject) - strlen('{EXCERPT}');
/* RFC recommends 78 characters for subjects */
$excerpt = $this->sanitizerService->tt($plainMessage, 78 - $subjectLength);
$message['subject'] = str_replace('{EXCERPT}', $excerpt, $message['subject']);
$subjectLengthRecommendByRFC = 78;
$excerpt = $this->sanitizerService->tt($plainMessage, $subjectLengthRecommendByRFC - $subjectLength);
$subject = str_replace('{EXCERPT}', $excerpt, $subject);
}
$mail->setSubject($message['subject']);
$mail->setSubject($subject);
}
private function setEmailTo(AsyncMail $mail, $to)
{
if (is_iterable($to)) {
foreach ($to as $recipient) {
$mail->addRecipient($recipient);
......@@ -108,8 +166,6 @@ final class EmailHelper
} else {
$mail->addRecipient($to);
}
$mail->send();
$this->metrics->addPoint('outgoing_email', ['template' => $tpl_id], ['count' => 1]);
}
public function validEmail(string $email): bool
......@@ -123,24 +179,7 @@ final class EmailHelper
public function libmail($bezirk, $email, $subject, $message, $attach = false, $token = false)
{
if ($bezirk === false) {
$bezirk = array(
'email' => DEFAULT_EMAIL,
'email_name' => DEFAULT_EMAIL_NAME
);
} elseif (!is_array($bezirk)) {
$bezirk = array(
'email' => $bezirk,
'email_name' => $bezirk
);
} else {
if (!$this->validEmail($bezirk['email'])) {
$bezirk['email'] = EMAIL_PUBLIC;
}
if (empty($bezirk['email_name'])) {
$bezirk['email_name'] = EMAIL_PUBLIC_NAME;
}
}
$bezirk = $this->normalizeBezirk($bezirk);
if (!$this->validEmail($email)) {
return false;
......@@ -156,15 +195,43 @@ final class EmailHelper
$htmlBody = $this->emailBodyTpl($message, $email, $token);
$mail->setHTMLBody($htmlBody);
$plainBody = $this->sanitizerService->htmlToPlain($htmlBody);
$mail->setBody($plainBody);
$this->setEmailBody($mail, $htmlBody);
$this->addAttachmentsToMail($mail, $attach);
$mail->send();
}
private function normalizeBezirk($bezirk)
{
if ($bezirk === false) {
$bezirk = [
'email' => DEFAULT_EMAIL,
'email_name' => DEFAULT_EMAIL_NAME,
];
} elseif (!is_array($bezirk)) {
$bezirk = [
'email' => $bezirk,
'email_name' => $bezirk,
];
} else {
if (!$this->validEmail($bezirk['email'])) {
$bezirk['email'] = EMAIL_PUBLIC;
}
if (empty($bezirk['email_name'])) {
$bezirk['email_name'] = EMAIL_PUBLIC_NAME;
}
}
return $bezirk;
}
private function addAttachmentsToMail(AsyncMail $mail, $attach)
{
if ($attach !== false) {
foreach ($attach as $a) {
$mail->addAttachment(new fFile($a['path']), $a['name']);
}
}
$mail->send();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment