Skip to content

Commit 5c545bd

Browse files
mdoutreluingnejaviereguiluz
authored andcommitted
[Mailer] add new events
1 parent 74e436b commit 5c545bd

File tree

1 file changed

+70
-13
lines changed

1 file changed

+70
-13
lines changed

mailer.rst

Lines changed: 70 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,33 +1410,90 @@ Mailer Events
14101410
MessageEvent
14111411
~~~~~~~~~~~~
14121412

1413+
**Event Class**: :class:`Symfony\\Component\\Mailer\\Event\\MessageEvent`
1414+
14131415
``MessageEvent`` allows to change the Message and the Envelope before the email
14141416
is sent::
14151417

14161418
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
14171419
use Symfony\Component\Mailer\Event\MessageEvent;
14181420
use Symfony\Component\Mime\Email;
14191421

1420-
class MailerSubscriber implements EventSubscriberInterface
1422+
public function onMessage(MessageEvent $event): void
14211423
{
1422-
public static function getSubscribedEvents()
1423-
{
1424-
return [
1425-
MessageEvent::class => 'onMessage',
1426-
];
1424+
$message = $event->getMessage();
1425+
if (!$message instanceof Email) {
1426+
return;
14271427
}
1428+
// do something with the message
1429+
}
14281430

1429-
public function onMessage(MessageEvent $event): void
1430-
{
1431-
$message = $event->getMessage();
1432-
if (!$message instanceof Email) {
1433-
return;
1434-
}
1431+
Execute this command to find out which listeners are registered for this event and
1432+
their priorities:
1433+
1434+
.. code-block:: terminal
1435+
1436+
$ php bin/console debug:event-dispatcher "Symfony\Component\Mailer\Event\MessageEvent"
1437+
1438+
SentMessageEvent
1439+
~~~~~~~~~~~~
1440+
1441+
**Event Class**: :class:`Symfony\\Component\\Mailer\\Event\\SentMessageEvent`
1442+
1443+
``SentMessageEvent`` it allows you to act on the :class:`Symfony\\Component\\\Mailer\\\SentMessage` to access the original
1444+
message (getOriginalMessage()) and some debugging information (getDebug()) such as
1445+
the HTTP calls made by the HTTP transports, which is useful for debugging errors::
1446+
1447+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1448+
use Symfony\Component\Mailer\Event\SentMessageEvent;
1449+
use Symfony\Component\Mailer\SentMessage;
14351450

1436-
// do something with the message
1451+
public function onMessage(SentMessageEvent $event): void
1452+
{
1453+
$message = $event->getMessage();
1454+
if (!$message instanceof SentMessage) {
1455+
return;
14371456
}
1457+
1458+
// do something with the message
1459+
}
1460+
1461+
Execute this command to find out which listeners are registered for this event and
1462+
their priorities:
1463+
1464+
.. code-block:: terminal
1465+
1466+
$ php bin/console debug:event-dispatcher "Symfony\Component\Mailer\Event\SentMessageEvent"
1467+
1468+
FailedMessageEvent
1469+
~~~~~~~~~~~~
1470+
1471+
**Event Class**: :class:`Symfony\\Component\\Mailer\\Event\\FailedMessageEvent`
1472+
1473+
``FailedMessageEvent`` it allows acting on the the initial message in case of a failure::
1474+
1475+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1476+
use Symfony\Component\Mailer\Event\FailedMessageEvent;
1477+
1478+
public function onMessage(FailedMessageEvent $event): void
1479+
{
1480+
// e.g you can get more information on this error when sending an email.
1481+
$event->getError();
1482+
1483+
// do something with the message
14381484
}
14391485

1486+
Execute this command to find out which listeners are registered for this event and
1487+
their priorities:
1488+
1489+
.. code-block:: terminal
1490+
1491+
$ php bin/console debug:event-dispatcher "Symfony\Component\Mailer\Event\FailedMessageEvent"
1492+
1493+
.. versionadded:: 6.2
1494+
1495+
``SentMessageEvent`` and ``FailedMessageEvent`` were introduced in Symfony 6.2.
1496+
14401497
Development & Debugging
14411498
-----------------------
14421499

0 commit comments

Comments
 (0)