[TASK] Add MailUtility and default templates

This commit is contained in:
Philipp Dieter 2020-10-04 23:49:58 +02:00
parent 8a55fa7209
commit c9bd34eb23
3 changed files with 621 additions and 0 deletions

View File

@ -0,0 +1,207 @@
<?php
namespace Cjel\TemplatesAide\Utility;
/***
*
* This file is part of the "Templates Aide" Extension for TYPO3 CMS.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* (c) 2020 Philipp Dieter <philipp.dieter@attic-media.net>
*
***/
use TYPO3\CMS\Core\Mail\MailMessage;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extbase\Service\ImageService;
use TYPO3\CMS\Fluid\View\StandaloneView;
use TYPO3\CMS\Fluid\View\TemplatePaths;
/**
*
*/
class MailUtility
{
/**
* tages maildata, builds html and text mails an decides where to send them
* allows to intercept sender for testing
*
* @param string $target email or group identifier
* @param string $subject mail subject, prefixed by setting in ts
* @param array $data content for email, gets parsed in different ways
* @return void
*/
public static function sendMail(
$target,
$sender,
$subject,
$data,
$templateRootPaths = null,
$templateNameHtml = null,
$templateNameText = null
) {
if (!$templateRootPaths) {
$templatePaths = new TemplatePaths();
$templatePaths->fillDefaultsByPackageName('templates_aide');
$templateRootPaths = $templatePaths->getTemplateRootPaths();
}
if (!$templateNameHtml) {
$templateNameHtml = 'Mailing/DefaultHtml';
}
if (!$templateNameText) {
$templateNameText = 'Mailing/DefaultText';
}
$htmlView = GeneralUtility::makeInstance(StandaloneView::class);
$htmlView->setTemplateRootPaths($templateRootPaths);
$htmlView->setTemplate($templateNameHtml);
$textView = GeneralUtility::makeInstance(StandaloneView::class);
$textView->setTemplateRootPaths($templateRootPaths);
$textView->setTemplate($templateNameText);
$mail = GeneralUtility::makeInstance(MailMessage::class);
$mail->setFrom($sender);
$mail->setSubject($subject);
$bodydataText = [];
$bodydataHtml = [];
foreach ($data as $row) {
switch($row['type']) {
case 'text':
case 'headline':
$htmlRow = $row;
$htmlRow['data'] = preg_replace_callback(
'/\[.*\]/mU',
function($matches) {
foreach ($matches as $match) {
return preg_replace_callback(
'/\[(\S*)\s(.*)\]/mU',
function($matchesInner) {
return '<a href="'
. $matchesInner[1]
. '">'
. $matchesInner[2]
. '</a>';
},
$match
);
}
},
$htmlRow['data']
);
$textRow = $row;
$textRow['data'] = preg_replace_callback(
'/\[.*\]/mU',
function($matches) {
foreach ($matches as $match) {
return preg_replace_callback(
'/\[(\S*)\s(.*)\]/mU',
function($matchesInner) {
return $matchesInner[2]
. ': '
. $matchesInner[1];
},
$match
);
}
},
$textRow['data']
);
$bodydataText[] = $textRow;
$bodydataHtml[] = $htmlRow;
break;
case 'button':
case 'buttons':
$htmlRow = $row;
//$htmlRow['targets'] = preg_replace_callback(
// '/\[.*\]/mU',
// function($matches) {
// foreach ($matches as $match) {
// return preg_replace_callback(
// '/\[(\S*)\s(.*)\]/mU',
// function($matchesInner) {
// return $matchesInner;
// //return '<a href="'
// // . $matchesInner[1]
// // . '">'
// // . $matchesInner[2]
// // . '</a>';
// },
// $match
// );
// }
// },
// $htmlRow['targets']
//);
$textRow = $row;
//$textRow['targets'] = preg_replace_callback(
// '/\[.*\]/mU',
// function($matches) {
// foreach ($matches as $match) {
// return preg_replace_callback(
// '/\[(\S*)\s(.*)\]/mU',
// function($matchesInner) {
// return $matchesInner;
// //return $matchesInner[2]
// // . ': '
// // . $matchesInner[1];
// },
// $match
// );
// }
// },
// $textRow['targets']
//);
$bodydataText[] = $textRow;
$bodydataHtml[] = $htmlRow;
break;
case 'attachmentBase64':
$attachmentdata = explode(',', $row['data']);
preg_match('/\w*:(.*);\w*/', $attachmentdata[0], $matches);
$mimetype = $matches[1];
preg_match('/\w*\/(.*);\w*/', $attachmentdata[0], $matches);
$fileextension = $matches[1];
$mail->attach(new \Swift_Attachment(
base64_decode($attachmentdata[1]),
'attachment.' . $fileextension,
$mimetype
));
break;
}
}
$textView->assign('content', $bodydataText);
$htmlView->assign('content', $bodydataHtml);
$domain = GeneralUtility::locationHeaderUrl('/');
$htmlView->assign('domain', $domain);
$textBody = $textView->render();
$htmlBody = $htmlView->render();
$mail->setBody($textBody);
$mail->addPart($htmlBody, 'text/html');
$recipients = explode(
',',
$target
);
if ($GLOBALS['TYPO3_CONF_VARS']['MAIL']['intercept_to']) {
$subjectOrig = $mail->getSubject();
$recipientsIntercecpted = explode(
',',
$GLOBALS['TYPO3_CONF_VARS']['MAIL']['intercept_to']
);
foreach ($recipientsIntercecpted as $recipientIntercepted) {
foreach ($recipients as $recipient) {
$mail->setSubject(
$subjectOrig . ' [ORIG-TO: ' . trim($recipient) . ']'
);
$mail->setTo(trim($recipientIntercepted));
$mail->send();
}
}
} else {
foreach ($recipients as $recipient) {
$mail->setTo(trim($recipient));
$mail->send();
}
}
}
}

View File

@ -0,0 +1,394 @@
<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office">
<head>
<title>
</title>
<!--[if !mso]><!-- -->
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<!--<![endif]-->
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style type="text/css">
#outlook a { padding:0; }
.ReadMsgBody { width:100%; }
.ExternalClass { width:100%; }
.ExternalClass * { line-height:100%; }
body { margin:0;padding:0;-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%; }
table, td { border-collapse:collapse;mso-table-lspace:0pt;mso-table-rspace:0pt; }
img { border:0;height:auto;line-height:100%; outline:none;text-decoration:none;-ms-interpolation-mode:bicubic; }
p { display:block;margin:13px 0; }
</style>
<!--[if !mso]><!-->
<style type="text/css">
@media only screen and (max-width:480px) {
@-ms-viewport { width:320px; }
@viewport { width:320px; }
}
</style>
<!--<![endif]-->
<!--[if mso]>
<xml>
<o:OfficeDocumentSettings>
<o:AllowPNG/>
<o:PixelsPerInch>96</o:PixelsPerInch>
</o:OfficeDocumentSettings>
</xml>
<![endif]-->
<!--[if lte mso 11]>
<style type="text/css">
.outlook-group-fix { width:100% !important; }
</style>
<![endif]-->
<style type="text/css">
@media only screen and (min-width:480px) {
.mj-column-per-100 { width:100% !important; max-width: 100%; }
.mj-column-per-50 { width:50% !important; max-width: 50%; }
}
</style>
<style type="text/css">
@media only screen and (max-width:480px) {
table.full-width-mobile { width: 100% !important; }
td.full-width-mobile { width: auto !important; }
}
</style>
</head>
<body style="background-color: #e0e0dc;">
<div style="">
<!--[if mso | IE]>
<table
align="center" border="0" cellpadding="0" cellspacing="0" class="" style="width:600px;" width="600"
>
<tr>
<td style="line-height:0px;font-size:0px;mso-line-height-rule:exactly;">
<![endif]-->
<div style="background:#ffffff;background-color:#ffffff;Margin:0px auto;max-width:600px;">
<table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="background:#ffffff;background-color:#ffffff;width:100%;">
<tbody>
<tr>
<td style="direction:ltr;font-size:0px;padding:0px 0 20px;padding-bottom:0;text-align:center;vertical-align:top;">
<!--[if mso | IE]>
<table role="presentation" border="0" cellpadding="0" cellspacing="0">
<tr>
<td
class="" style="vertical-align:top;width:600px;"
>
<![endif]-->
<div class="mj-column-per-100 outlook-group-fix" style="font-size:13px;text-align:left;direction:ltr;display:inline-block;vertical-align:top;width:100%;">
<table border="0" cellpadding="0" cellspacing="0" role="presentation" style="vertical-align:top;" width="100%">
<tr>
<td align="center" style="font-size:0px;padding:0px 0px 10px;word-break:break-word;">
<table border="0" cellpadding="0" cellspacing="0" role="presentation" style="border-collapse:collapse;border-spacing:0px;">
<tbody>
<tr>
<td style="width:600px;">
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</table>
</div>
<!--[if mso | IE]>
</td>
</tr>
</table>
<![endif]-->
</td>
</tr>
</tbody>
</table>
</div>
<!--[if mso | IE]>
</td>
</tr>
</table>
<f:for
each="{content}"
as="row"
key="rowKey"
iteration="rowI"
>
<v:condition.type.isArray value="{row.data}">
<f:then>
<table
align="center" border="0" cellpadding="0" cellspacing="0" class="" style="width:600px;" width="600"
>
<tr>
<td style="line-height:0px;font-size:0px;mso-line-height-rule:exactly;">
<![endif]-->
<div style="background:#ffffff;background-color:#ffffff;Margin:0px auto;max-width:600px;">
<table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="background:#ffffff;background-color:#ffffff;width:100%;">
<tbody>
<tr>
<td style="direction:ltr;font-size:0px;padding:2px 32px;text-align:center;vertical-align:top;">
<!--[if mso | IE]>
<table role="presentation" border="0" cellpadding="0" cellspacing="0">
<tr>
<td
class="" style="vertical-align:top;width:268px;"
>
<![endif]-->
<div class="mj-column-per-50 outlook-group-fix" style="font-size:13px;text-align:left;direction:ltr;display:inline-block;vertical-align:top;width:100%;">
<table border="0" cellpadding="0" cellspacing="0" role="presentation" style="vertical-align:top;" width="100%">
<tr>
<td align="left" style="font-size:0px;padding:2px 4px;word-break:break-word;">
<div style="font-family:Arial, sans-serif;font-size:16px;line-height:1;text-align:left;color:#000000;">
<div>{row.data.0 -> f:format.nl2br() -> f:format.raw()}</div>
</div>
</td>
</tr>
</table>
</div>
<!--[if mso | IE]>
</td>
<td
class="" style="vertical-align:top;width:268px;"
>
<![endif]-->
<div class="mj-column-per-50 outlook-group-fix" style="font-size:13px;text-align:left;direction:ltr;display:inline-block;vertical-align:top;width:100%;">
<table border="0" cellpadding="0" cellspacing="0" role="presentation" style="vertical-align:top;" width="100%">
<tr>
<td align="left" style="font-size:0px;padding:2px 4px;word-break:break-word;">
<div style="font-family:Arial, sans-serif;font-size:16px;line-height:1;text-align:left;color:#000000;">
<div>{row.data.1 -> f:format.nl2br() -> f:format.raw()}</div>
</div>
</td>
</tr>
</table>
</div>
<!--[if mso | IE]>
</td>
</tr>
</table>
<![endif]-->
</td>
</tr>
</tbody>
</table>
</div>
<!--[if mso | IE]>
</td>
</tr>
</table>
</f:then>
<f:else>
<f:if condition="{row.type} == 'headline' || {row.type} == 'text'">
<table
align="center" border="0" cellpadding="0" cellspacing="0" class="" style="width:600px;" width="600"
>
<tr>
<td style="line-height:0px;font-size:0px;mso-line-height-rule:exactly;">
<![endif]-->
<div style="background:#ffffff;background-color:#ffffff;Margin:0px auto;max-width:600px;">
<table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="background:#ffffff;background-color:#ffffff;width:100%;">
<tbody>
<tr>
<td style="direction:ltr;font-size:0px;padding:0 0;padding-top:0;text-align:center;vertical-align:top;">
<!--[if mso | IE]>
<table role="presentation" border="0" cellpadding="0" cellspacing="0">
<tr>
<td
class="" style="vertical-align:top;width:600px;"
>
<![endif]-->
<div class="mj-column-per-100 outlook-group-fix" style="font-size:13px;text-align:left;direction:ltr;display:inline-block;vertical-align:top;width:100%;">
<table border="0" cellpadding="0" cellspacing="0" role="presentation" style="vertical-align:top;" width="100%">
<tr>
<td align="left" style="font-size:0px;padding:0 34px;word-break:break-word;">
<div style="font-family:Arial, sans-serif;font-size:16px;line-height:22px;text-align:left;color:#000000;">
<f:if condition="{row.type} == 'headline'">
<f:then>
<h2>{row.data -> f:format.nl2br() -> f:format.raw()}</h2>
</f:then>
<f:else>
<p>{row.data -> f:format.nl2br() -> f:format.raw()}</p>
</f:else>
</f:if>
</div>
</td>
</tr>
</table>
</div>
<!--[if mso | IE]>
</td>
</tr>
</table>
<![endif]-->
</td>
</tr>
</tbody>
</table>
</div>
<!--[if mso | IE]>
</td>
</tr>
</table>
</f:if>
<f:if condition="{row.type} == 'button'">
<table
align="center" border="0" cellpadding="0" cellspacing="0" class="" style="width:600px;" width="600"
>
<tr>
<td style="line-height:0px;font-size:0px;mso-line-height-rule:exactly;">
<![endif]-->
<div style="background:#ffffff;background-color:#ffffff;Margin:0px auto;max-width:600px;">
<table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="background:#ffffff;background-color:#ffffff;width:100%;">
<tbody>
<tr>
<td style="direction:ltr;font-size:0px;padding:2px 32px;text-align:center;vertical-align:top;">
<!--[if mso | IE]>
<table role="presentation" border="0" cellpadding="0" cellspacing="0">
<tr>
<td
class="" style="vertical-align:top;width:268px;"
>
<![endif]-->
<div class="mj-column-per-50 outlook-group-fix" style="font-size:13px;text-align:left;direction:ltr;display:inline-block;vertical-align:top;width:100%;">
<table border="0" cellpadding="0" cellspacing="0" role="presentation" style="vertical-align:top;" width="100%">
<tr>
<td align="center" vertical-align="middle" class="button" style="background-color: #FFFFFF; font-size: 0px; padding: 20px 10px; word-break: break-word;" bgcolor="#FFFFFF">
<table border="0" cellpadding="0" cellspacing="0" role="presentation" style="border-collapse:separate;line-height:100%;">
<tr>
<td align="center" bgcolor="#4667A7" role="presentation" style="background-color: #4667A7; border: none; border-radius: 0; cursor: auto; mso-padding-alt: 0; background: #4667A7;" valign="middle">
<a href="{row.targets.0.0}" style="display: inline-block; background: #4667A7; color: #ffffff; font-family: Arial, sans-serif; font-size: 14px; font-weight: normal; line-height: 120%; margin: 0; text-decoration: none; text-transform: none; padding: 0; mso-padding-alt: 0px; border-radius: 0;" target="_blank">
<span class="button-inner" style="padding: 15px 42px; display: block; border: 1px solid #4667A7; color: #ffffff; font-size: 14px; font-weight: bold;">{row.targets.0.1}</span>
</a>
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
<!--[if mso | IE]>
</td>
</tr>
</table>
<![endif]-->
</td>
</tr>
</tbody>
</table>
</div>
<!--[if mso | IE]>
</td>
</tr>
</table>
</f:if>
<f:if condition="{row.type} == 'buttons'">
<table
align="center" border="0" cellpadding="0" cellspacing="0" class="" style="width:600px;" width="600"
>
<tr>
<td style="line-height:0px;font-size:0px;mso-line-height-rule:exactly;">
<![endif]-->
<div style="background:#ffffff;background-color:#ffffff;Margin:0px auto;max-width:600px;">
<table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="background:#ffffff;background-color:#ffffff;width:100%;">
<tbody>
<tr>
<td style="direction:ltr;font-size:0px;padding:2px 32px;text-align:center;vertical-align:top;">
<!--[if mso | IE]>
<table role="presentation" border="0" cellpadding="0" cellspacing="0">
<tr>
<td
class="" style="vertical-align:top;width:268px;"
>
<![endif]-->
<div class="mj-column-per-50 outlook-group-fix" style="font-size:13px;text-align:left;direction:ltr;display:inline-block;vertical-align:top;width:100%;">
<table border="0" cellpadding="0" cellspacing="0" role="presentation" style="vertical-align:top;" width="100%">
<tr>
<td align="center" vertical-align="middle" class="button" style="background-color: #FFFFFF; font-size: 0px; padding: 20px 10px; word-break: break-word;" bgcolor="#FFFFFF">
<table border="0" cellpadding="0" cellspacing="0" role="presentation" style="border-collapse:separate;line-height:100%;">
<tr>
<td align="center" bgcolor="#4667A7" role="presentation" style="background-color: #4667A7; border: none; border-radius: 0; cursor: auto; mso-padding-alt: 0; background: #4667A7;" valign="middle">
<a href="{row.targets.0.0}" style="display: inline-block; background: #4667A7; color: #ffffff; font-family: Arial, sans-serif; font-size: 14px; font-weight: normal; line-height: 120%; margin: 0; text-decoration: none; text-transform: none; padding: 0; mso-padding-alt: 0px; border-radius: 0;" target="_blank">
<span class="button-inner" style="padding: 15px 42px; display: block; border: 1px solid #4667A7; color: #ffffff; font-size: 14px; font-weight: bold;">{row.targets.0.1}</span>
</a>
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
<!--[if mso | IE]>
</td>
<td
class="" style="vertical-align:top;width:268px;"
>
<![endif]-->
<div class="mj-column-per-50 outlook-group-fix" style="font-size:13px;text-align:left;direction:ltr;display:inline-block;vertical-align:top;width:100%;">
<table border="0" cellpadding="0" cellspacing="0" role="presentation" style="vertical-align:top;" width="100%">
<tr>
<td align="center" vertical-align="middle" class="button" style="background-color: #FFFFFF; font-size: 0px; padding: 20px 10px; word-break: break-word;" bgcolor="#FFFFFF">
<table border="0" cellpadding="0" cellspacing="0" role="presentation" style="border-collapse:separate;line-height:100%;">
<tr>
<td align="center" bgcolor="#4667A7" role="presentation" style="background-color: #4667A7; border: none; border-radius: 0; cursor: auto; mso-padding-alt: 0; background: #4667A7;" valign="middle">
<a href="{row.targets.1.0}" style="display: inline-block; background: #4667A7; color: #ffffff; font-family: Arial, sans-serif; font-size: 14px; font-weight: normal; line-height: 120%; margin: 0; text-decoration: none; text-transform: none; padding: 0; mso-padding-alt: 0px; border-radius: 0;" target="_blank">
<span class="button-inner" style="padding: 15px 42px; display: block; border: 1px solid #4667A7; color: #ffffff; font-size: 14px; font-weight: bold;">{row.targets.1.1}</span>
</a>
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
<!--[if mso | IE]>
</td>
</tr>
</table>
<![endif]-->
</td>
</tr>
</tbody>
</table>
</div>
<!--[if mso | IE]>
</td>
</tr>
</table>
</f:if>
</f:else>
</v:condition.type.isArray>
</f:for>
<table
align="center" border="0" cellpadding="0" cellspacing="0" class="" style="width:600px;" width="600"
>
<tr>
<td style="line-height:0px;font-size:0px;mso-line-height-rule:exactly;">
<![endif]-->
<div style="background:#ffffff;background-color:#ffffff;Margin:0px auto;max-width:600px;">
<table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="background:#ffffff;background-color:#ffffff;width:100%;">
<tbody>
<tr>
<td style="direction:ltr;font-size:0px;padding:20px;text-align:center;vertical-align:top;">
<!--[if mso | IE]>
<table role="presentation" border="0" cellpadding="0" cellspacing="0">
<tr>
</tr>
</table>
<![endif]-->
</td>
</tr>
</tbody>
</table>
</div>
<!--[if mso | IE]>
</td>
</tr>
</table>
<![endif]-->
</div>
</body>
</html>

View File

@ -0,0 +1,20 @@
<v:variable.set name="br">
</v:variable.set>
<f:for
each="{content}"
as="row"
key="rowKey"
iteration="rowI"
><v:format.trim>
<v:condition.type.isArray value="{row.data}">
<f:then>{row.data.0}: {row.data.1}</f:then>
<f:else><f:if condition="{row.type} == 'headline' || {row.type} == 'text'"><f:if condition="{row.type} == 'headline'">
<f:then>= {row.data} =</f:then>
<f:else>{row.data}</f:else>
</f:if></f:if></f:else>
</v:condition.type.isArray>
</v:format.trim><f:if condition="{content.{rowI.cycle}.data}">{br}<f:if condition="{v:condition.type.isArray(value='{content.{rowI.index}.data}', then: '1')} && {v:condition.type.isArray(value='{content.{rowI.cycle}.data}', then: '1')}"><f:else>{br}</f:else></f:if></f:if><f:if condition="{row.type} == 'buttons'">
<f:then>{br}{br}{row.targets.0.1}:{br}{row.targets.0.0}{br}{br}{row.targets.1.1}:{br}{row.targets.1.0}</f:then>
</f:if><f:if condition="{row.type} == 'button'">
<f:then>{br}{br}{row.targets.0.1}:{br}{row.targets.0.0}</f:then>
</f:if></f:for>