Skip to content

Commit 2af761d

Browse files
committed
Merge branch '6.4' into 7.2
* 6.4: [Mailer] Mention the SentMessageEvent and FailedMessageEvent in the debug section
2 parents e020bd9 + 6d9da13 commit 2af761d

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

mailer.rst

+23-7
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,8 @@ Catch that exception to recover from the error or to display some message::
824824
// error message or try to resend the message
825825
}
826826

827+
.. _mailer-debugging-emails:
828+
827829
Debugging Emails
828830
----------------
829831

@@ -833,6 +835,10 @@ provides access to the original message (``getOriginalMessage()``) and to some
833835
debug information (``getDebug()``) such as the HTTP calls done by the HTTP
834836
transports, which is useful to debug errors.
835837

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+
836842
.. note::
837843

838844
If your code used :class:`Symfony\\Component\\Mailer\\MailerInterface`, you
@@ -1701,24 +1707,26 @@ and their priorities:
17011707
17021708
$ php bin/console debug:event-dispatcher "Symfony\Component\Mailer\Event\MessageEvent"
17031709
1710+
.. _mailer-sent-message-event:
1711+
17041712
SentMessageEvent
17051713
~~~~~~~~~~~~~~~~
17061714

17071715
**Event Class**: :class:`Symfony\\Component\\Mailer\\Event\\SentMessageEvent`
17081716

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

17141722
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
17151723
use Symfony\Component\Mailer\Event\SentMessageEvent;
17161724

17171725
public function onMessage(SentMessageEvent $event): void
17181726
{
1719-
$message = $event->getMessage();
1727+
$message $event->getMessage();
17201728

1721-
// do something with the message
1729+
// do something with the message (e.g. get its id)
17221730
}
17231731

17241732
Execute this command to find out which listeners are registered for this event
@@ -1728,20 +1736,28 @@ and their priorities:
17281736
17291737
$ php bin/console debug:event-dispatcher "Symfony\Component\Mailer\Event\SentMessageEvent"
17301738
1739+
.. _mailer-failed-message-event:
1740+
17311741
FailedMessageEvent
17321742
~~~~~~~~~~~~~~~~~~
17331743

17341744
**Event Class**: :class:`Symfony\\Component\\Mailer\\Event\\FailedMessageEvent`
17351745

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

17381750
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
17391751
use Symfony\Component\Mailer\Event\FailedMessageEvent;
1752+
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
17401753

17411754
public function onMessage(FailedMessageEvent $event): void
17421755
{
17431756
// 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+
}
17451761

17461762
// do something with the message
17471763
}

0 commit comments

Comments
 (0)