[TASK] Imrove mail content template parsing capabilities

This commit is contained in:
Philipp Dieter 2021-05-02 13:16:15 +02:00
parent 75be493b60
commit c4f9c16ff7
3 changed files with 66 additions and 13 deletions

View File

@ -26,6 +26,49 @@ use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
*/ */
class MailUtility class MailUtility
{ {
/**
* Parse text with a simple "template system" to be used as data for
* sendMail function
*
* @param string $text
* @param array $markers
* @return array
*/
public static function parseContentTemplate(
$text,
$markers = []
) {
$textParts = explode("\r\n\r\n", $text);
$result = [];
foreach ($textParts as $textPart) {
$type = 'text';
if (substr($textPart, 0, 2) === '# ') {
$type = 'headline';
$textPart = substr($textPart, 2);
}
if (substr($textPart, 0, 3) === '## ') {
$type = 'headline2';
$textPart = substr($textPart, 3);
}
if (substr($textPart, 0, 4) === '### ') {
$type = 'headline3';
$textPart = substr($textPart, 4);
}
foreach ($markers as $markerName => $markerContent) {
$textPart = str_replace(
'###' . $markerName . '###',
$markerContent,
$textPart
);
}
$result[] = [
'type' => $type,
'data' => $textPart,
];
}
return $result;
}
/** /**
* tages maildata, builds html and text mails an decides where to send them * tages maildata, builds html and text mails an decides where to send them
* allows to intercept sender for testing * allows to intercept sender for testing
@ -88,6 +131,8 @@ class MailUtility
switch($row['type']) { switch($row['type']) {
case 'text': case 'text':
case 'headline': case 'headline':
case 'headline2':
case 'headline3':
$htmlRow = $row; $htmlRow = $row;
$htmlRow['data'] = preg_replace_callback( $htmlRow['data'] = preg_replace_callback(
'/\[.*\]/mU', '/\[.*\]/mU',

View File

@ -3,7 +3,7 @@
<f:then> <f:then>
</f:then> </f:then>
<f:else> <f:else>
<f:if condition="{row.type} == 'headline' || {row.type} == 'text'"> <f:if condition="{v:condition.string.contains(haystack: '{row.type}', needle: 'headline', then: '1')} || {row.type} == 'text'">
<!--[if mso | IE]> <!--[if mso | IE]>
<table align="center" border="0" cellpadding="0" cellspacing="0" class="" style="width:640px;" width="640" > <table align="center" border="0" cellpadding="0" cellspacing="0" class="" style="width:640px;" width="640" >
<tr> <tr>
@ -13,7 +13,7 @@
<table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="background:#ffffff;background-color:#ffffff;width:100%;"> <table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="background:#ffffff;background-color:#ffffff;width:100%;">
<tbody> <tbody>
<tr> <tr>
<td style="direction:ltr;font-size:0px;padding:20px;text-align:center;"> <td style="direction:ltr;font-size:0px;padding:0 20px;text-align:center;">
<!--[if mso | IE]> <!--[if mso | IE]>
<table role="presentation" border="0" cellpadding="0" cellspacing="0"> <table role="presentation" border="0" cellpadding="0" cellspacing="0">
<tr> <tr>
@ -27,15 +27,21 @@
<table border="0" cellpadding="0" cellspacing="0" role="presentation" style width="100%"> <table border="0" cellpadding="0" cellspacing="0" role="presentation" style width="100%">
<tr> <tr>
<td align="left" style="font-size:0px;padding:0;word-break:break-word;"> <td align="left" style="font-size:0px;padding:0;word-break:break-word;">
<div style="font-family:Arial, sans-serif;font-size:16px;line-height:1;text-align:left;color:#000000;"> <div style="font-family:Arial, sans-serif;font-size:16px;line-height:1.4;text-align:left;color:#000000;">
<f:if condition="{row.type} == 'headline'"> <f:switch expression="{row.type}">
<f:then> <f:case value="headline">
<h1>{row.data -> f:format.nl2br() -> f:format.raw()}</h1>
</f:case>
<f:case value="headline2">
<h2>{row.data -> f:format.nl2br() -> f:format.raw()}</h2> <h2>{row.data -> f:format.nl2br() -> f:format.raw()}</h2>
</f:then> </f:case>
<f:else> <f:case value="headline3">
<h3>{row.data -> f:format.nl2br() -> f:format.raw()}</h3>
</f:case>
<f:defaultCase>
<p>{row.data -> f:format.nl2br() -> f:format.raw()}</p> <p>{row.data -> f:format.nl2br() -> f:format.raw()}</p>
</f:else> </f:defaultCase>
</f:if> </f:switch>
</div> </div>
</td> </td>
</tr> </tr>

View File

@ -8,10 +8,12 @@
><v:format.trim> ><v:format.trim>
<v:condition.type.isArray value="{row.data}"> <v:condition.type.isArray value="{row.data}">
<f:then>{row.data.0}: {row.data.1}</f:then> <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:else><f:if condition="{v:condition.string.contains(haystack: '{row.type}', needle: 'headline', then: '1')} || {row.type} == 'text'"><f:switch expression="{row.type}">
<f:then>= {row.data} =</f:then> <f:case value="headline">= {row.data} =</f:case>
<f:else>{row.data}</f:else> <f:case value="headline2">== {row.data} ==</f:case>
</f:if></f:if></f:else> <f:case value="headline3">=== {row.data} ===</f:case>
<f:defaultCase>{row.data}</f:defaultCase>
</f:switch></f:if></f:else>
</v:condition.type.isArray> </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'"> </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: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>