BeforeMailerSentMessageEvent

New in version 12.0.

This event is dispatched before the message is sent by the mailer and can be used to manipulate the Symfony\Component\Mime\RawMessage and the Symfony\Component\Mailer\Envelope. Usually a Symfony\Component\Mime\Email or TYPO3\CMS\Core\Mail\FluidEmail instance is given as RawMessage. Additionally the mailer instance is given, which depends on the implementation - usually TYPO3\CMS\Core\Mail\Mailer. It contains the Symfony\Component\Mailer\Transport object, which can be retrieved using the getTransport() method.

Example

Registration of the event listener:

EXT:my_extension/Configuration/Services.yaml
MyVendor\MyExtension\EventListener\MailerSentMessageEventListener:
  tags:
    - name: event.listener
      identifier: 'my-extension/modify-message'
      method: 'modifyMessage'

The corresponding event listener class:

EXT:my_extension/Classes/EventListener/MailerSentMessageEventListener.php
namespace MyVendor\MyExtension\EventListener;

use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use TYPO3\CMS\Core\Mail\Event\BeforeMailerSentMessageEvent;

final class MailerSentMessageEventListener
{
    public function modifyMessage(BeforeMailerSentMessageEvent $event): void
    {
        $message = $event->getMessage();

        // If $message is an Email implementation, add an additional recipient
        if ($message instanceof Email) {
            $message->addCc(new Address('cc_recipient@example.org'));
        }
    }
 }

API

class TYPO3\CMS\Core\Mail\Event\BeforeMailerSentMessageEvent

This event is fired before the Mailer has sent a message and allows listeners to manipulate the RawMessage and the Envelope.

Note: Usually TYPO3CMSCoreMailMailer is given to the event. This implementation allows to retrieve the TransportInterface using the getTransport() method.

getMessage()
Return type

Symfony\Component\Mime\RawMessage

setMessage(Symfony\\Component\\Mime\\RawMessage $message)
Parameters
  • $message (Symfony\Component\Mime\RawMessage) -- the message

getEnvelope()
Return type

Symfony\Component\Mailer\Envelope

setEnvelope(Symfony\\Component\\Mailer\\Envelope $envelope = NULL)
Parameters
  • $envelope (Symfony\Component\Mailer\Envelope) -- the envelope, default: NULL

getMailer()
Return type

Symfony\Component\Mailer\MailerInterface