@@ -802,6 +802,8 @@ Catch that exception to recover from the error or to display some message::
802
802
// error message or try to resend the message
803
803
}
804
804
805
+ .. _mailer-debugging-emails :
806
+
805
807
Debugging Emails
806
808
----------------
807
809
@@ -810,6 +812,9 @@ The :class:`Symfony\\Component\\Mailer\\SentMessage` object returned by the
810
812
provides access to the original message (``getOriginalMessage() ``) and to some
811
813
debug information (``getDebug() ``) such as the HTTP calls done by the HTTP
812
814
transports, which is useful to debug errors.
815
+ Access to :class: `Symfony\\ Component\\ Mailer\\ SentMessage ` can also be obtained by listening
816
+ to the :ref: `SentMessageEvent <mailer-sent-message-event >`, and to ``getDebug() `` by listening
817
+ to the :ref: `FailedMessageEvent <mailer-failed-message-event >`."
813
818
814
819
.. note ::
815
820
@@ -1712,6 +1717,8 @@ and their priorities:
1712
1717
1713
1718
$ php bin/console debug:event-dispatcher "Symfony\Component\Mailer\Event\MessageEvent"
1714
1719
1720
+ .. _mailer-sent-message-event :
1721
+
1715
1722
SentMessageEvent
1716
1723
~~~~~~~~~~~~~~~~
1717
1724
@@ -1722,16 +1729,17 @@ SentMessageEvent
1722
1729
The ``SentMessageEvent `` event was introduced in Symfony 6.2.
1723
1730
1724
1731
``SentMessageEvent `` allows you to act on the :class: `Symfony\\ Component\\\M ailer\\\S entMessage `
1725
- class to access the original message (``getOriginalMessage() ``) and some debugging
1726
- information (``getDebug() ``) such as the HTTP calls made by the HTTP transports,
1727
- which is useful for debugging errors::
1732
+ class to access the original message (``getOriginalMessage() ``) and some
1733
+ :ref: ` debugging information < mailer-debugging-emails >` (``getDebug() ``) such as the HTTP calls
1734
+ made by the HTTP transports, which is useful for debugging errors::
1728
1735
1729
1736
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1730
1737
use Symfony\Component\Mailer\Event\SentMessageEvent;
1731
1738
1732
1739
public function onMessage(SentMessageEvent $event): void
1733
1740
{
1734
- $message = $event->getMessage();
1741
+ // e.g you can get mail id
1742
+ $event->getMessage();
1735
1743
1736
1744
// do something with the message
1737
1745
}
@@ -1743,6 +1751,8 @@ and their priorities:
1743
1751
1744
1752
$ php bin/console debug:event-dispatcher "Symfony\Component\Mailer\Event\SentMessageEvent"
1745
1753
1754
+ .. _mailer-failed-message-event :
1755
+
1746
1756
FailedMessageEvent
1747
1757
~~~~~~~~~~~~~~~~~~
1748
1758
@@ -1752,15 +1762,21 @@ FailedMessageEvent
1752
1762
1753
1763
The ``FailedMessageEvent `` event was introduced in Symfony 6.2.
1754
1764
1755
- ``FailedMessageEvent `` allows acting on the initial message in case of a failure::
1765
+ ``FailedMessageEvent `` allows acting on the initial message in case of a failure and some
1766
+ :ref: `debugging information <mailer-debugging-emails >` (``getDebug() ``) such as the HTTP calls made
1767
+ by the HTTP transports, which is useful for debugging errors::
1756
1768
1757
1769
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1758
1770
use Symfony\Component\Mailer\Event\FailedMessageEvent;
1771
+ use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
1759
1772
1760
1773
public function onMessage(FailedMessageEvent $event): void
1761
1774
{
1762
1775
// e.g you can get more information on this error when sending an email
1763
- $event->getError();
1776
+ $error = $event->getError();
1777
+ if ($error instanceof TransportExceptionInterface) {
1778
+ $error->getDebug();
1779
+ }
1764
1780
1765
1781
// do something with the message
1766
1782
}
0 commit comments