This discussion is about a new feature which makes it easier to generate styled emails and to have identical email layouts through the whole system.
Current state of sending an email looks like this
$mailMessage = GeneralUtility::makeInstance(MailMessage::class); $mailMessage ->addTo($recipient) ->addFrom($this->getSenderEmailAddress(), $this->getSenderEmailName()) ->setSubject($this->getEmailSubject()) ->setBody('<html><body>html test content</body></html>', 'text/html') ->addPart('plain test content', 'text/plain') ->send();
The hard part is to generate a nice HTML and plain content. Especially if creating a lot of styled mails, people are using Fluid’s
StandaloneView to generate the content. Everyone is creating his own API which is tedious and should be changed
The new API extends
\TYPO3\CMS\Core\Mail\MailMessage by various methods which could look like this
$variables = ['title' => 'My title']; $templatedEmail = GeneralUtility::makeInstance(TemplatedEmail::class); $templatedEmail->addTo('firstname.lastname@example.org') ->addFrom('email@example.com', 'Test') ->setSubject('A mail') ->addContentAsFluidTemplateFile('EXT:fo/<path-to-file>.html', $variables, TemplatedEmail::FORMAT_HTML) ->send();
A proof of concept extension implementing this can be found at github.com/georgringer/templatedMail
The global configuration is done by the site configuration
templatedEmail: templateRootPath: EXT:templatedmail/Resources/Private/Templates/ partialRootPath: EXT:templatedmail/Resources/Private/Partials/ layoutRootPath: EXT:templatedmail/Resources/Private/Layouts/
Every extension can of course override the paths during usage of
TemplatedEmail by using
If emails need to be sent in various languages, there are basically the following options:
If the amount of text which needs to be translated is not much, the ViewHelper
<f translate language="fr" /> can be used.
Different templates per language
Instead of translating only parts of a mail, it is also possible to use a different template file per language. This could be done by using
By using partials for non-translatable text (e.g. technical data) there would be no need to have duplicated content in various files.
The API could support developers by providing:
- a method to set the available language which would be injected as variable into the fluid templates
- use the language information to find the proper template. E.g. providing
frand the if the template with suffix
_fris available, that would be used.
StandaloneViewis provided by the core, so developers need less code themselves
- All templates can share the same layouts which makes it possible to style all mails by changing 1 Layout file, no matter if it is an email sent by the core (e.g. Install Tool) or by a 3rd party extension (as soon as those are adopted to the new API)
Instead of using
MailMessage, the new API can be used.
Remarks and notes
Currently not part of this discussion
- Inlining CSS into HTML, e.g. by using https://packagist.org/packages/tijsverkoyen/css-to-inline-styles
- Generating plain text mails automatically based on the HTML mail
Topic Initiator: Georg Ringer