Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Mailer] Mention the SentMessageEvent and FailedMessageEvent #20186

Merged
merged 1 commit into from
Feb 3, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions mailer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,8 @@ Catch that exception to recover from the error or to display some message::
// error message or try to resend the message
}

.. _mailer-debugging-emails:

Debugging Emails
----------------

Expand All @@ -810,6 +812,9 @@ The :class:`Symfony\\Component\\Mailer\\SentMessage` object returned by the
provides access to the original message (``getOriginalMessage()``) and to some
debug information (``getDebug()``) such as the HTTP calls done by the HTTP
transports, which is useful to debug errors.
Access to :class:`Symfony\\Component\\Mailer\\SentMessage` can also be obtained by listening
to the :ref:`SentMessageEvent <mailer-sent-message-event>`, and to ``getDebug()`` by listening
to the :ref:`FailedMessageEvent <mailer-failed-message-event>`."

.. note::

Expand Down Expand Up @@ -1712,6 +1717,8 @@ and their priorities:

$ php bin/console debug:event-dispatcher "Symfony\Component\Mailer\Event\MessageEvent"

.. _mailer-sent-message-event:

SentMessageEvent
~~~~~~~~~~~~~~~~

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

``SentMessageEvent`` allows you to act on the :class:`Symfony\\Component\\\Mailer\\\SentMessage`
class to access the original message (``getOriginalMessage()``) and some debugging
information (``getDebug()``) such as the HTTP calls made by the HTTP transports,
which is useful for debugging errors::
class to access the original message (``getOriginalMessage()``) and some
:ref:`debugging information <mailer-debugging-emails>` (``getDebug()``) such as the HTTP calls
made by the HTTP transports, which is useful for debugging errors::

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Mailer\Event\SentMessageEvent;

public function onMessage(SentMessageEvent $event): void
{
$message = $event->getMessage();
// e.g you can get mail id
$event->getMessage();

// do something with the message
}
Expand All @@ -1743,6 +1751,8 @@ and their priorities:

$ php bin/console debug:event-dispatcher "Symfony\Component\Mailer\Event\SentMessageEvent"

.. _mailer-failed-message-event:

FailedMessageEvent
~~~~~~~~~~~~~~~~~~

Expand All @@ -1752,15 +1762,21 @@ FailedMessageEvent

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

``FailedMessageEvent`` allows acting on the initial message in case of a failure::
``FailedMessageEvent`` allows acting on the initial message in case of a failure and some
:ref:`debugging information <mailer-debugging-emails>` (``getDebug()``) such as the HTTP calls made
by the HTTP transports, which is useful for debugging errors::

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Mailer\Event\FailedMessageEvent;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;

public function onMessage(FailedMessageEvent $event): void
{
// e.g you can get more information on this error when sending an email
$event->getError();
$error = $event->getError();
if ($error instanceof TransportExceptionInterface) {
$error->getDebug();
}

// do something with the message
}
Expand Down