@@ -829,30 +829,56 @@ Catch that exception to recover from the error or to display some message::
829829Debugging Emails
830830----------------
831831
832- The :class: `Symfony\\ Component\\ Mailer\\ SentMessage ` object returned by the
833- ``send() `` method of the :class: `Symfony\\ Component\\ Mailer\\ Transport\\ TransportInterface `
834- provides access to the original message (``getOriginalMessage() ``) and to some
835- debug information (``getDebug() ``) such as the HTTP calls done by the HTTP
836- transports, which is useful to debug errors.
832+ The ``send() `` method of the mailer service injected when using ``MailerInterface ``
833+ doesn't return anything, so you can't access the sent email information. This is because
834+ it sends email messages **asynchronously ** when the :doc: `Messenger component </messenger >`
835+ is used in the application.
837836
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 >`.
837+ To access information about the sent email, update your code to replace the
838+ :class: ` Symfony \\ Component \\ Mailer \\ MailerInterface ` with
839+ :class: ` Symfony \\ Component \\ Mailer \\ Transport \\ TransportInterface `:
841840
842- .. note ::
841+ .. code-block :: diff
842+
843+ -use Symfony\Component\Mailer\MailerInterface;
844+ +use Symfony\Component\Mailer\Transport\TransportInterface;
845+ // ...
846+
847+ class MailerController extends AbstractController
848+ {
849+ #[Route('/email')]
850+ - public function sendEmail(MailerInterface $mailer): Response
851+ + public function sendEmail(TransportInterface $mailer): Response
852+ {
853+ $email = (new Email())
854+ // ...
855+
856+ $sentEmail = $mailer->send($email);
857+
858+ // ...
859+ }
860+ }
861+
862+ The ``send() `` method of ``TransportInterface `` returns an object of type
863+ :class: `Symfony\\ Component\\ Mailer\\ SentMessage `. This is because it always sends
864+ the emails **synchronously **, even if your application uses the Messenger component.
865+
866+ The ``SentMessage `` object provides access to the original message
867+ (``getOriginalMessage() ``) and to some debug information (``getDebug() ``) such
868+ as the HTTP calls done by the HTTP transports, which is useful to debug errors.
843869
844- If your code used :class: `Symfony\\ Component\\ Mailer\\ MailerInterface `, you
845- need to replace it by :class: ` Symfony \\ Component \\ Mailer \\ Transport \\ TransportInterface `
846- to have the `` SentMessage `` object returned .
870+ You can also access the :class: `Symfony\\ Component\\ Mailer\\ SentMessage ` object
871+ by listening to the :ref: ` SentMessageEvent < mailer-sent-message-event >`, and retrieve
872+ `` getDebug() `` by listening to the :ref: ` FailedMessageEvent < mailer-failed-message-event >` .
847873
848874.. note ::
849875
850876 Some mailer providers change the ``Message-Id `` when sending the email. The
851- ``getMessageId() `` method from ``SentMessage `` always returns the definitive
852- ID of the message (being the original random ID generated by Symfony or the
853- new ID generated by the mailer provider) .
877+ ``getMessageId() `` method from ``SentMessage `` always returns the final ID
878+ of the message - whether it's the original random ID generated by Symfony or
879+ a new one generated by the provider.
854880
855- The exceptions related to mailer transports (those which implement
881+ Exceptions related to mailer transports (those implementing
856882:class: `Symfony\\ Component\\ Mailer\\ Exception\\ TransportException `) also provide
857883this debug information via the ``getDebug() `` method.
858884
0 commit comments