Skip to content

Commit d4d6a96

Browse files
Add event subscriber example to mailer documentation
1 parent 2235259 commit d4d6a96

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

mailer.rst

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,43 @@ 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+
The mailer component also throws a events:
1028+
1029+
MessageEvent
1030+
~~~~~~~~~~~~
1031+
1032+
The MessageEvent allows you to manipulate the send message:
1033+
1034+
.. code-block:: php
1035+
1036+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1037+
use Symfony\Component\Mailer\Event\MessageEvent;
1038+
use Symfony\Component\Mime\Email;
1039+
1040+
class MailerListener implements EventSubscriberInterface
1041+
{
1042+
public static function getSubscribedEvents()
1043+
{
1044+
return [
1045+
MessageEvent::class => 'onMessage',
1046+
];
1047+
}
1048+
1049+
public function onMessage(MessageEvent $event): void
1050+
{
1051+
$message = $event->getMessage();
1052+
if (!$message instanceof Email) {
1053+
return;
1054+
}
1055+
1056+
// do something with the message
1057+
}
1058+
}
1059+
1060+
10241061
Development & Debugging
10251062
-----------------------
10261063

0 commit comments

Comments
 (0)