@@ -875,30 +875,56 @@ Catch that exception to recover from the error or to display some message::
875
875
Debugging Emails
876
876
----------------
877
877
878
- The :class: `Symfony\\ Component\\ Mailer\\ SentMessage ` object returned by the
879
- ``send() `` method of the :class: `Symfony\\ Component\\ Mailer\\ Transport\\ TransportInterface `
880
- provides access to the original message (``getOriginalMessage() ``) and to some
881
- debug information (``getDebug() ``) such as the HTTP calls done by the HTTP
882
- transports, which is useful to debug errors.
878
+ The ``send() `` method of the mailer service injected when using ``MailerInterface ``
879
+ doesn't return anything, so you can't access the sent email information. This is because
880
+ it sends email messages **asynchronously ** when the :doc: `Messenger component </messenger >`
881
+ is used in the application.
883
882
884
- You can also access :class: ` Symfony \\ Component \\ Mailer \\ SentMessage ` by listening
885
- to the :ref: ` SentMessageEvent < mailer-sent-message-event >` and retrieve `` getDebug() ``
886
- by listening to the :ref: ` FailedMessageEvent < mailer-failed-message-event >`.
883
+ To access information about the sent email, update your code to replace the
884
+ :class: ` Symfony \\ Component \\ Mailer \\ MailerInterface ` with
885
+ :class: ` Symfony \\ Component \\ Mailer \\ Transport \\ TransportInterface `:
887
886
888
- .. note ::
887
+ .. code-block :: diff
888
+
889
+ -use Symfony\Component\Mailer\MailerInterface;
890
+ +use Symfony\Component\Mailer\Transport\TransportInterface;
891
+ // ...
892
+
893
+ class MailerController extends AbstractController
894
+ {
895
+ #[Route('/email')]
896
+ - public function sendEmail(MailerInterface $mailer): Response
897
+ + public function sendEmail(TransportInterface $mailer): Response
898
+ {
899
+ $email = (new Email())
900
+ // ...
901
+
902
+ $sentEmail = $mailer->send($email);
903
+
904
+ // ...
905
+ }
906
+ }
907
+
908
+ The ``send() `` method of ``TransportInterface `` returns an object of type
909
+ :class: `Symfony\\ Component\\ Mailer\\ SentMessage `. This is because it always sends
910
+ the emails **synchronously **, even if your application uses the Messenger component.
911
+
912
+ The ``SentMessage `` object provides access to the original message
913
+ (``getOriginalMessage() ``) and to some debug information (``getDebug() ``) such
914
+ as the HTTP calls done by the HTTP transports, which is useful to debug errors.
889
915
890
- If your code used :class: `Symfony\\ Component\\ Mailer\\ MailerInterface `, you
891
- need to replace it by :class: ` Symfony \\ Component \\ Mailer \\ Transport \\ TransportInterface `
892
- to have the `` SentMessage `` object returned .
916
+ You can also access the :class: `Symfony\\ Component\\ Mailer\\ SentMessage ` object
917
+ by listening to the :ref: ` SentMessageEvent < mailer-sent-message-event >`, and retrieve
918
+ `` getDebug() `` by listening to the :ref: ` FailedMessageEvent < mailer-failed-message-event >` .
893
919
894
920
.. note ::
895
921
896
922
Some mailer providers change the ``Message-Id `` when sending the email. The
897
- ``getMessageId() `` method from ``SentMessage `` always returns the definitive
898
- ID of the message (being the original random ID generated by Symfony or the
899
- new ID generated by the mailer provider) .
923
+ ``getMessageId() `` method from ``SentMessage `` always returns the final ID
924
+ of the message - whether it's the original random ID generated by Symfony or
925
+ a new one generated by the provider.
900
926
901
- The exceptions related to mailer transports (those which implement
927
+ Exceptions related to mailer transports (those implementing
902
928
:class: `Symfony\\ Component\\ Mailer\\ Exception\\ TransportException `) also provide
903
929
this debug information via the ``getDebug() `` method.
904
930
0 commit comments