@@ -824,6 +824,8 @@ Catch that exception to recover from the error or to display some message::
824
824
// error message or try to resend the message
825
825
}
826
826
827
+ .. _mailer-debugging-emails :
828
+
827
829
Debugging Emails
828
830
----------------
829
831
@@ -833,6 +835,10 @@ provides access to the original message (``getOriginalMessage()``) and to some
833
835
debug information (``getDebug() ``) such as the HTTP calls done by the HTTP
834
836
transports, which is useful to debug errors.
835
837
838
+ You can also access :class: `Symfony\\ Component\\ Mailer\\ SentMessage ` by listening
839
+ to the :ref: `SentMessageEvent <mailer-sent-message-event >` and retrieve ``getDebug() ``
840
+ by listening to the :ref: `FailedMessageEvent <mailer-failed-message-event >`.
841
+
836
842
.. note ::
837
843
838
844
If your code used :class: `Symfony\\ Component\\ Mailer\\ MailerInterface `, you
@@ -1701,24 +1707,26 @@ and their priorities:
1701
1707
1702
1708
$ php bin/console debug:event-dispatcher "Symfony\Component\Mailer\Event\MessageEvent"
1703
1709
1710
+ .. _mailer-sent-message-event :
1711
+
1704
1712
SentMessageEvent
1705
1713
~~~~~~~~~~~~~~~~
1706
1714
1707
1715
**Event Class **: :class: `Symfony\\ Component\\ Mailer\\ Event\\ SentMessageEvent `
1708
1716
1709
1717
``SentMessageEvent `` allows you to act on the :class: `Symfony\\ Component\\\M ailer\\\S entMessage `
1710
- class to access the original message (``getOriginalMessage() ``) and some debugging
1711
- information (``getDebug() ``) such as the HTTP calls made by the HTTP transports,
1712
- which is useful for debugging errors::
1718
+ class to access the original message (``getOriginalMessage() ``) and some
1719
+ :ref: ` debugging information < mailer-debugging-emails >` (``getDebug() ``) such as
1720
+ the HTTP calls made by the HTTP transports, which is useful for debugging errors::
1713
1721
1714
1722
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1715
1723
use Symfony\Component\Mailer\Event\SentMessageEvent;
1716
1724
1717
1725
public function onMessage(SentMessageEvent $event): void
1718
1726
{
1719
- $message = $event->getMessage();
1727
+ $message $event->getMessage();
1720
1728
1721
- // do something with the message
1729
+ // do something with the message (e.g. get its id)
1722
1730
}
1723
1731
1724
1732
Execute this command to find out which listeners are registered for this event
@@ -1728,20 +1736,28 @@ and their priorities:
1728
1736
1729
1737
$ php bin/console debug:event-dispatcher "Symfony\Component\Mailer\Event\SentMessageEvent"
1730
1738
1739
+ .. _mailer-failed-message-event :
1740
+
1731
1741
FailedMessageEvent
1732
1742
~~~~~~~~~~~~~~~~~~
1733
1743
1734
1744
**Event Class **: :class: `Symfony\\ Component\\ Mailer\\ Event\\ FailedMessageEvent `
1735
1745
1736
- ``FailedMessageEvent `` allows acting on the initial message in case of a failure::
1746
+ ``FailedMessageEvent `` allows acting on the initial message in case of a failure
1747
+ and some :ref: `debugging information <mailer-debugging-emails >` (``getDebug() ``)
1748
+ such as the HTTP calls made by the HTTP transports, which is useful for debugging errors::
1737
1749
1738
1750
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1739
1751
use Symfony\Component\Mailer\Event\FailedMessageEvent;
1752
+ use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
1740
1753
1741
1754
public function onMessage(FailedMessageEvent $event): void
1742
1755
{
1743
1756
// e.g you can get more information on this error when sending an email
1744
- $event->getError();
1757
+ $error = $event->getError();
1758
+ if ($error instanceof TransportExceptionInterface) {
1759
+ $error->getDebug();
1760
+ }
1745
1761
1746
1762
// do something with the message
1747
1763
}
0 commit comments