@@ -876,6 +876,8 @@ Catch that exception to recover from the error or to display some message::
876
876
// error message or try to resend the message
877
877
}
878
878
879
+ .. _mailer-debugging-emails :
880
+
879
881
Debugging Emails
880
882
----------------
881
883
@@ -885,6 +887,10 @@ provides access to the original message (``getOriginalMessage()``) and to some
885
887
debug information (``getDebug() ``) such as the HTTP calls done by the HTTP
886
888
transports, which is useful to debug errors.
887
889
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
+
888
894
.. note ::
889
895
890
896
If your code used :class: `Symfony\\ Component\\ Mailer\\ MailerInterface `, you
@@ -1753,24 +1759,26 @@ and their priorities:
1753
1759
1754
1760
$ php bin/console debug:event-dispatcher "Symfony\Component\Mailer\Event\MessageEvent"
1755
1761
1762
+ .. _mailer-sent-message-event :
1763
+
1756
1764
SentMessageEvent
1757
1765
~~~~~~~~~~~~~~~~
1758
1766
1759
1767
**Event Class **: :class: `Symfony\\ Component\\ Mailer\\ Event\\ SentMessageEvent `
1760
1768
1761
1769
``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::
1765
1773
1766
1774
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1767
1775
use Symfony\Component\Mailer\Event\SentMessageEvent;
1768
1776
1769
1777
public function onMessage(SentMessageEvent $event): void
1770
1778
{
1771
- $message = $event->getMessage();
1779
+ $message $event->getMessage();
1772
1780
1773
- // do something with the message
1781
+ // do something with the message (e.g. get its id)
1774
1782
}
1775
1783
1776
1784
Execute this command to find out which listeners are registered for this event
@@ -1780,20 +1788,28 @@ and their priorities:
1780
1788
1781
1789
$ php bin/console debug:event-dispatcher "Symfony\Component\Mailer\Event\SentMessageEvent"
1782
1790
1791
+ .. _mailer-failed-message-event :
1792
+
1783
1793
FailedMessageEvent
1784
1794
~~~~~~~~~~~~~~~~~~
1785
1795
1786
1796
**Event Class **: :class: `Symfony\\ Component\\ Mailer\\ Event\\ FailedMessageEvent `
1787
1797
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::
1789
1801
1790
1802
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1791
1803
use Symfony\Component\Mailer\Event\FailedMessageEvent;
1804
+ use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
1792
1805
1793
1806
public function onMessage(FailedMessageEvent $event): void
1794
1807
{
1795
1808
// 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
+ }
1797
1813
1798
1814
// do something with the message
1799
1815
}
0 commit comments