Skip to content

[Mailer] add new events #17181

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

Merged
merged 1 commit into from
Sep 20, 2022
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
83 changes: 70 additions & 13 deletions mailer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1410,33 +1410,90 @@ Mailer Events
MessageEvent
~~~~~~~~~~~~

**Event Class**: :class:`Symfony\\Component\\Mailer\\Event\\MessageEvent`

``MessageEvent`` allows to change the Message and the Envelope before the email
is sent::

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Mailer\Event\MessageEvent;
use Symfony\Component\Mime\Email;

class MailerSubscriber implements EventSubscriberInterface
public function onMessage(MessageEvent $event): void
{
public static function getSubscribedEvents()
{
return [
MessageEvent::class => 'onMessage',
];
$message = $event->getMessage();
if (!$message instanceof Email) {
return;
}
// do something with the message
}

public function onMessage(MessageEvent $event): void
{
$message = $event->getMessage();
if (!$message instanceof Email) {
return;
}
Execute this command to find out which listeners are registered for this event and
their priorities:

.. code-block:: terminal
$ php bin/console debug:event-dispatcher "Symfony\Component\Mailer\Event\MessageEvent"
SentMessageEvent
~~~~~~~~~~~~

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

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

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

// do something with the message
public function onMessage(SentMessageEvent $event): void
{
$message = $event->getMessage();
if (!$message instanceof SentMessage) {
return;
}

// do something with the message
}

Execute this command to find out which listeners are registered for this event and
their priorities:

.. code-block:: terminal
$ php bin/console debug:event-dispatcher "Symfony\Component\Mailer\Event\SentMessageEvent"
FailedMessageEvent
~~~~~~~~~~~~

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

``FailedMessageEvent`` it allows acting on the the initial message in case of a failure::

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

public function onMessage(FailedMessageEvent $event): void
{
// e.g you can get more information on this error when sending an email.
$event->getError();
// do something with the message
}

Execute this command to find out which listeners are registered for this event and
their priorities:

.. code-block:: terminal
$ php bin/console debug:event-dispatcher "Symfony\Component\Mailer\Event\FailedMessageEvent"
.. versionadded:: 6.2

``SentMessageEvent`` and ``FailedMessageEvent`` were introduced in Symfony 6.2.

Development & Debugging
-----------------------

Expand Down