@@ -807,40 +807,59 @@ Catch that exception to recover from the error or to display some message::
807807Debugging Emails
808808----------------
809809
810- The :class: `Symfony\\ Component\\ Mailer\\ SentMessage ` object returned by the
811- ``send() `` method of the :class: `Symfony\\ Component\\ Mailer\\ Transport\\ TransportInterface `
812- provides access to the original message (``getOriginalMessage() ``) and to some
813- debug information (``getDebug() ``) such as the HTTP calls done by the HTTP
814- transports, which is useful to debug errors.
810+ The ``send() `` method of the mailer service injected when using ``MailerInterface ``
811+ doesn't return anything, so you can't access the sent email information. This is because
812+ it sends email messages **asynchronously ** when the :doc: `Messenger component </messenger >`
813+ is used in the application.
815814
816- You can also access :class: ` Symfony \\ Component \\ Mailer \\ SentMessage ` by listening
817- to the :ref: ` SentMessageEvent < mailer-sent-message-event >` and retrieve `` getDebug() ``
818- by listening to the :ref: ` FailedMessageEvent < mailer-failed-message-event >`.
815+ To access information about the sent email, update your code to replace the
816+ :class: ` Symfony \\ Component \\ Mailer \\ MailerInterface ` with
817+ :class: ` Symfony \\ Component \\ Mailer \\ Transport \\ TransportInterface `:
819818
820- .. note ::
819+ .. code-block :: diff
820+
821+ -use Symfony\Component\Mailer\MailerInterface;
822+ +use Symfony\Component\Mailer\Transport\TransportInterface;
823+ // ...
824+
825+ class MailerController extends AbstractController
826+ {
827+ #[Route('/email')]
828+ - public function sendEmail(MailerInterface $mailer): Response
829+ + public function sendEmail(TransportInterface $mailer): Response
830+ {
831+ $email = (new Email())
832+ // ...
833+
834+ $sentEmail = $mailer->send($email);
821835
822- If your code used :class: `Symfony\\ Component\\ Mailer\\ MailerInterface `, you
823- need to replace it by :class: `Symfony\\ Component\\ Mailer\\ Transport\\ TransportInterface `
824- to have the ``SentMessage `` object returned.
836+ // ...
837+ }
838+ }
839+
840+ The ``send() `` method of ``TransportInterface `` returns an object of type
841+ :class: `Symfony\\ Component\\ Mailer\\ SentMessage `. This is because it always sends
842+ the emails **synchronously **, even if your application uses the Messenger component.
843+
844+ The ``SentMessage `` object provides access to the original message
845+ (``getOriginalMessage() ``) and to some debug information (``getDebug() ``) such
846+ as the HTTP calls done by the HTTP transports, which is useful to debug errors.
847+
848+ You can also access the :class: `Symfony\\ Component\\ Mailer\\ SentMessage ` object
849+ by listening to the :ref: `SentMessageEvent <mailer-sent-message-event >`, and retrieve
850+ ``getDebug() `` by listening to the :ref: `FailedMessageEvent <mailer-failed-message-event >`.
825851
826852.. note ::
827853
828854 Some mailer providers change the ``Message-Id `` when sending the email. The
829- ``getMessageId() `` method from ``SentMessage `` always returns the definitive
830- ID of the message (being the original random ID generated by Symfony or the
831- new ID generated by the mailer provider) .
855+ ``getMessageId() `` method from ``SentMessage `` always returns the final ID
856+ of the message - whether it's the original random ID generated by Symfony or
857+ a new one generated by the provider.
832858
833- The exceptions related to mailer transports (those which implement
859+ Exceptions related to mailer transports (those implementing
834860:class: `Symfony\\ Component\\ Mailer\\ Exception\\ TransportException `) also provide
835861this debug information via the ``getDebug() `` method.
836862
837- But you have to keep in mind that using :class: `Symfony\\ Component\\ Mailer\\ Transport\\ TransportInterface `
838- you can't rely on asynchronous sending emails.
839- It doesn't use a bus to dispatch :class: `Symfony\\ Component\\ Mailer\\ Messenger\\ SendEmailMessage `.
840-
841- Use :class: `Symfony\\ Component\\ Mailer\\ MailerInterface ` if you want to have an opportunity
842- to send emails asynchronously.
843-
844863.. _mailer-twig :
845864
846865Twig: HTML & CSS
0 commit comments