Skip to content

Commit 70fa4c0

Browse files
alexander-schranzjaviereguiluz
authored andcommitted
Add event subscriber example to mailer documentation
1 parent 2235259 commit 70fa4c0

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

mailer.rst

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,39 @@ you have a transport called ``async``, you can route the message there:
10211021
Thanks to this, instead of being delivered immediately, messages will be sent to
10221022
the transport to be handled later (see :ref:`messenger-worker`).
10231023

1024+
Events
1025+
------
1026+
1027+
MessageEvent
1028+
~~~~~~~~~~~~
1029+
1030+
The MessageEvent allows the transformation of a Message and the Envelope before the email is sent::
1031+
1032+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1033+
use Symfony\Component\Mailer\Event\MessageEvent;
1034+
use Symfony\Component\Mime\Email;
1035+
1036+
class MailerSubscriber implements EventSubscriberInterface
1037+
{
1038+
public static function getSubscribedEvents()
1039+
{
1040+
return [
1041+
MessageEvent::class => 'onMessage',
1042+
];
1043+
}
1044+
1045+
public function onMessage(MessageEvent $event): void
1046+
{
1047+
$message = $event->getMessage();
1048+
if (!$message instanceof Email) {
1049+
return;
1050+
}
1051+
1052+
// do something with the message
1053+
}
1054+
}
1055+
1056+
10241057
Development & Debugging
10251058
-----------------------
10261059

0 commit comments

Comments
 (0)