New in version 10.3: TYPO3 now supports sending template-based emails for multi-part and HTML-based
emails out-of-the-box. The email contents are built with the Fluid Templating Engine.
Feature: #90266 - Fluid-based email templating

TYPO3 CMS provides a RFC-compliant mailing solution based on
symfony/mailer
for sending emails and
symfony/mime
for creating email messages.

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.

This doesn’t send any mail out, but instead will write every outgoing mail to a file
adhering to the RFC 4155 mbox format, which is a simple text file where the mails are
concatenated. Useful for debugging the mail sending process and on development machines
which cannot send mails to the outside. The file to write to is defined by:

This sends an email using an existing Fluid template TipsAndTricks.html.
Make sure the paths are setup as described in Fluid paths.

$email=GeneralUtility::makeInstance(FluidEmail::class);$email->to('contact@acme.com')->from(newAddress('jeremy@acme.com','Jeremy'))->subject('TYPO3 loves you - here is why')->setFormat('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 Fluid template:

<f:sectionname="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:

// Create the message$mail=\TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Mail\MailMessage::class);// Prepare and send the message$mail// Defining the "From" email address and name as an object// (email clients will display the name)->from(new\Symfony\Component\Mime\Address('john@doe.com','John Doe'))// Set the "To" addresses->to(new\Symfony\Component\Mime\Address('receiver@example.org','Max Mustermann'),new\Symfony\Component\Mime\Address('other@domain.org'))// Give the message a subject->subject('Your subject')// Give it the text message->text('Here is the message itself')// And optionally a HTML message->html('<q>Here is the message itself</q>')// Optionally add any attachments->attachFromPath('/path/to/my-document.pdf')// And finally send it->send();

Or if you prefer, don’t concatenate the calls:

$mail=\TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Mail\MailMessage::class);$mail->from(new\Symfony\Component\Mime\Address('john@doe.com','John Doe'));$mail->to(new\Symfony\Component\Mime\Address('receiver@example.org','Max Mustermann'),new\Symfony\Component\Mime\Address('other@domain.org'));$mail->subject('Your subject');$mail->text('Here is the message itself');$mail->html('<q>Here is the message itself</q>');$mail->attachFromPath('/path/to/my-document.pdf');$mail->send();

Note

Before TYPO3 v10 the MailMessage class only had methods like
->setTo(), setFrom(), ->setSubject() etc.
Now the class inherits from \Symfony\Component\Mime\Email which
provides the methods from the example. To make migration from older TYPO3
versions easier the previous methods still exist. The use of
MailMessage in own extensions is recommended.

// Attach file to message$mail->attachFromPath('/path/to/documents/privacy.pdf');// Optionally you can tell email clients to display a custom name for the file$mail->attachFromPath('/path/to/documents/privacy.pdf','Privacy Policy');// Alternatively attach contents from a stream$mail->attach(fopen('/path/to/documents/contract.doc','r'));

$from=\TYPO3\CMS\Core\Utility\MailUtility::getSystemFrom();$mail=\TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Mail\MailMessage::class);// As getSystemFrom() returns an array we need to use the setFrom method$mail->setFrom($from);// ...$mail->send();

In case of the problem “Mails are not sent” in your extension, try to set a
ReturnPath:. Start as before but add:

// you will get a valid Email Adress from 'defaultMailFromAddress' or if not set from PHP settings or from system.// if result is not a valid email, the final result will be no-reply@example.com..$returnPath=\TYPO3\CMS\Core\Utility\MailUtility::getSystemFromAddress();if($returnPath!="no-reply@example.com"){$mail->setReturnPath($returnPath);}$mail->send();