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
Example
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('reciepient@example.org')
->addFrom('noreply@fo.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
Global Configuration
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 $templatedEmail->setTemplateRootPaths(['<path>']);
Translated mails
If emails need to be sent in various languages, there are basically the following options:
Translated strings
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
->addContentAsFluidTemplateFile('pathToFile_Fr>.html')
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
fr
and the if the template with suffix_fr
is available, that would be used.
Impact
- Fluid’s
StandaloneView
is 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)
Possible Migrations
Instead of using ->setBody
by MailMessage
, the new API can be used.
Remarks and notes
Currently not part of this discussion
- Inlining CSS into HTML, e.g. by using tijsverkoyen/css-to-inline-styles - Packagist
- Generating plain text mails automatically based on the HTML mail
Organizational
Topic Initiator: Georg Ringer