Skip to content

Commit 8e3c1db

Browse files
wkaniajaviereguiluz
authored andcommitted
[Mailer] Mention the SentMessageEvent and FailedMessageEvent in the debug section
1 parent 21dffc6 commit 8e3c1db

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

mailer.rst

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,8 @@ Catch that exception to recover from the error or to display some message::
802802
// error message or try to resend the message
803803
}
804804

805+
.. _mailer-debugging-emails:
806+
805807
Debugging Emails
806808
----------------
807809

@@ -810,6 +812,9 @@ The :class:`Symfony\\Component\\Mailer\\SentMessage` object returned by the
810812
provides access to the original message (``getOriginalMessage()``) and to some
811813
debug information (``getDebug()``) such as the HTTP calls done by the HTTP
812814
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>`."
813818

814819
.. note::
815820

@@ -1712,6 +1717,8 @@ and their priorities:
17121717
17131718
$ php bin/console debug:event-dispatcher "Symfony\Component\Mailer\Event\MessageEvent"
17141719
1720+
.. _mailer-sent-message-event:
1721+
17151722
SentMessageEvent
17161723
~~~~~~~~~~~~~~~~
17171724

@@ -1722,16 +1729,17 @@ SentMessageEvent
17221729
The ``SentMessageEvent`` event was introduced in Symfony 6.2.
17231730

17241731
``SentMessageEvent`` allows you to act on the :class:`Symfony\\Component\\\Mailer\\\SentMessage`
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::
17281735

17291736
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
17301737
use Symfony\Component\Mailer\Event\SentMessageEvent;
17311738

17321739
public function onMessage(SentMessageEvent $event): void
17331740
{
1734-
$message = $event->getMessage();
1741+
// e.g you can get mail id
1742+
$event->getMessage();
17351743

17361744
// do something with the message
17371745
}
@@ -1743,6 +1751,8 @@ and their priorities:
17431751
17441752
$ php bin/console debug:event-dispatcher "Symfony\Component\Mailer\Event\SentMessageEvent"
17451753
1754+
.. _mailer-failed-message-event:
1755+
17461756
FailedMessageEvent
17471757
~~~~~~~~~~~~~~~~~~
17481758

@@ -1752,15 +1762,21 @@ FailedMessageEvent
17521762

17531763
The ``FailedMessageEvent`` event was introduced in Symfony 6.2.
17541764

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::
17561768

17571769
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
17581770
use Symfony\Component\Mailer\Event\FailedMessageEvent;
1771+
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
17591772

17601773
public function onMessage(FailedMessageEvent $event): void
17611774
{
17621775
// 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+
}
17641780

17651781
// do something with the message
17661782
}

0 commit comments

Comments
 (0)