From 70fa4c0af8cc5a3dfde5d185a1d5e6f1531b972f Mon Sep 17 00:00:00 2001 From: Alexander Schranz Date: Fri, 22 Apr 2022 11:25:28 +0200 Subject: [PATCH] Add event subscriber example to mailer documentation --- mailer.rst | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/mailer.rst b/mailer.rst index 9f26c769d82..97eae836c00 100644 --- a/mailer.rst +++ b/mailer.rst @@ -1021,6 +1021,39 @@ you have a transport called ``async``, you can route the message there: Thanks to this, instead of being delivered immediately, messages will be sent to the transport to be handled later (see :ref:`messenger-worker`). +Events +------ + +MessageEvent +~~~~~~~~~~~~ + +The MessageEvent allows the transformation of a Message and the Envelope before the email is sent:: + + use Symfony\Component\EventDispatcher\EventSubscriberInterface; + use Symfony\Component\Mailer\Event\MessageEvent; + use Symfony\Component\Mime\Email; + + class MailerSubscriber implements EventSubscriberInterface + { + public static function getSubscribedEvents() + { + return [ + MessageEvent::class => 'onMessage', + ]; + } + + public function onMessage(MessageEvent $event): void + { + $message = $event->getMessage(); + if (!$message instanceof Email) { + return; + } + + // do something with the message + } + } + + Development & Debugging -----------------------