Feature: #90266 - Fluid-based email templating
See forge#90266
Description
TYPO3 now supports sending template-based emails for multi-part and HTML-based emails out-of-the-box. The email contents are built with Fluid Templating Engine.
TYPO3's backend functionality already ships with a default layout for templated emails, which can be tested out in TYPO3's install tool test email functionality.
It is also possible to set a default mode for sending out emails via $GLOBALS
which can be both
, plain
or html
.
This option can however overridden by Extension authors in their use cases.
All Fluid-based template paths can be configured via
Local
:
$GLOBALS
['TYPO3_ CONF_ VARS'] ['MAIL'] ['layout Root Paths'] $GLOBALS
['TYPO3_ CONF_ VARS'] ['MAIL'] ['partial Root Paths'] $GLOBALS
['TYPO3_ CONF_ VARS'] ['MAIL'] ['template Root Paths']
where TYPO3 reserves all array keys below 100
for internal purposes. If you want to provide custom templates or layouts,
set this in your Local
/ Additional
file:
$GLOBALS
['TYPO3_ CONF_ VARS'] ['MAIL'] ['template Root Paths'] [700] = 'EXT: my_ site_ extension/ Resources/ Private/ Templates/ Email'; $GLOBALS
['TYPO3_ CONF_ VARS'] ['MAIL'] ['layout Root Paths'] [700] = 'EXT: my_ site_ extension/ Resources/ Private/ Layouts';
In addition, it is possible to define a section within the Fluid template,
which - if set - takes precedence over the subject
method.
Impact
TYPO3 now sends out templated messages for system emails in both plaintext and HTML format.
It is possible to use the same API in your custom extension like this:
$email = GeneralUtility::makeInstance(FluidEmail::class);
$email
->to('contact@acme.com')
->from(new Address('jeremy@acme.com', 'Jeremy'))
->subject('TYPO3 loves you - here is why')
->format('html') // only HTML mail
->setTemplate('TipsAndTricks')
->assign('mySecretIngredient', 'Tomato and TypoScript');
GeneralUtility::makeInstance(Mailer::class)->send($email);
Defining a custom email subject in a custom template:
<f:section name="Subject">New Login at "{typo3.sitename}"</f:section>
Building templated emails with Fluid also allows to define the language key, and use this within the Fluid template:
$email = GeneralUtility::makeInstance(FluidEmail::class);
$email
->to('contact@acme.com')
->assign('language', 'de');
<f:translate languageKey="{language}" id="LLL:my_ext/Resources/Private/Language/emails.xml:subject" />