@@ -876,6 +876,8 @@ Catch that exception to recover from the error or to display some message::
876876 // error message or try to resend the message
877877 }
878878
879+ .. _mailer-debugging-emails :
880+
879881Debugging Emails
880882----------------
881883
@@ -885,6 +887,10 @@ provides access to the original message (``getOriginalMessage()``) and to some
885887debug information (``getDebug() ``) such as the HTTP calls done by the HTTP
886888transports, which is useful to debug errors.
887889
890+ You can also access :class: `Symfony\\ Component\\ Mailer\\ SentMessage ` by listening
891+ to the :ref: `SentMessageEvent <mailer-sent-message-event >` and retrieve ``getDebug() ``
892+ by listening to the :ref: `FailedMessageEvent <mailer-failed-message-event >`.
893+
888894.. note ::
889895
890896 If your code used :class: `Symfony\\ Component\\ Mailer\\ MailerInterface `, you
@@ -1753,24 +1759,26 @@ and their priorities:
17531759
17541760 $ php bin/console debug:event-dispatcher "Symfony\Component\Mailer\Event\MessageEvent"
17551761
1762+ .. _mailer-sent-message-event :
1763+
17561764SentMessageEvent
17571765~~~~~~~~~~~~~~~~
17581766
17591767**Event Class **: :class: `Symfony\\ Component\\ Mailer\\ Event\\ SentMessageEvent `
17601768
17611769``SentMessageEvent `` allows you to act on the :class: `Symfony\\ Component\\\M ailer\\\S entMessage `
1762- class to access the original message (``getOriginalMessage() ``) and some debugging
1763- information (``getDebug() ``) such as the HTTP calls made by the HTTP transports,
1764- which is useful for debugging errors::
1770+ class to access the original message (``getOriginalMessage() ``) and some
1771+ :ref: ` debugging information < mailer-debugging-emails >` (``getDebug() ``) such as
1772+ the HTTP calls made by the HTTP transports, which is useful for debugging errors::
17651773
17661774 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
17671775 use Symfony\Component\Mailer\Event\SentMessageEvent;
17681776
17691777 public function onMessage(SentMessageEvent $event): void
17701778 {
1771- $message = $event->getMessage();
1779+ $message $event->getMessage();
17721780
1773- // do something with the message
1781+ // do something with the message (e.g. get its id)
17741782 }
17751783
17761784Execute this command to find out which listeners are registered for this event
@@ -1780,20 +1788,28 @@ and their priorities:
17801788
17811789 $ php bin/console debug:event-dispatcher "Symfony\Component\Mailer\Event\SentMessageEvent"
17821790
1791+ .. _mailer-failed-message-event :
1792+
17831793FailedMessageEvent
17841794~~~~~~~~~~~~~~~~~~
17851795
17861796**Event Class **: :class: `Symfony\\ Component\\ Mailer\\ Event\\ FailedMessageEvent `
17871797
1788- ``FailedMessageEvent `` allows acting on the initial message in case of a failure::
1798+ ``FailedMessageEvent `` allows acting on the initial message in case of a failure
1799+ and some :ref: `debugging information <mailer-debugging-emails >` (``getDebug() ``)
1800+ such as the HTTP calls made by the HTTP transports, which is useful for debugging errors::
17891801
17901802 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
17911803 use Symfony\Component\Mailer\Event\FailedMessageEvent;
1804+ use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
17921805
17931806 public function onMessage(FailedMessageEvent $event): void
17941807 {
17951808 // e.g you can get more information on this error when sending an email
1796- $event->getError();
1809+ $error = $event->getError();
1810+ if ($error instanceof TransportExceptionInterface) {
1811+ $error->getDebug();
1812+ }
17971813
17981814 // do something with the message
17991815 }
0 commit comments