Skip to content

Commit c4b06d8

Browse files
committed
minor #16744 Add event subscriber example to mailer documentation (alexander-schranz)
This PR was squashed before being merged into the 4.4 branch. Discussion ---------- Add event subscriber example to mailer documentation I could not find something about events in the docs but could find the following event in the code. Commits ------- 70fa4c0 Add event subscriber example to mailer documentation
2 parents 11abc81 + 70fa4c0 commit c4b06d8

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
@@ -1024,6 +1024,39 @@ you have a transport called ``async``, you can route the message there:
10241024
Thanks to this, instead of being delivered immediately, messages will be sent to
10251025
the transport to be handled later (see :ref:`messenger-worker`).
10261026

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

0 commit comments

Comments
 (0)