phpsymfonymailer

Symfony Mailer: Message lost context data when captured on MessageSent event


I'm trying to capture the email sent from my Symfony 6.3 application using an event listener for SentMessageEvent. The issue I'm facing is that the context, which I set on the email when creating it (using TemplatedEmail), appears to be empty when the onMessageSent method is triggered.

Here is how I send the email:

<?php

use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mailer\MailerInterface;

readonly class EmailService
{
    public function __construct(
        private Mailer $mailer,
    ) {
    }

    public function sendEmail(): void
    {
        $email = (new TemplatedEmail())
            ->to('at@example.com')
            ->from('from@example.fr')
            ->subject('My subject')
            ->htmlTemplate('email/email.html.twig')
            ->context([
                'data' => [
                    'key' => 'value'
                ]
            ]);

        $this->mailer->send($email);
    }

}

Here's my event listener:

<?php

namespace App\EventListener;

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

class MailEventListener implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [
            SentMessageEvent::class => 'onMessageSent',
            MessageEvent::class => 'onMessage',
        ];
    }

    public function onMessage(MessageEvent $event): void
    {
        // Here I can access the context
        dump($event->getMessage());
        // Here is the output, the context contains the values setted when the TemplatedEmail Object
        /*
        Symfony\Bridge\Twig\Mime\TemplatedEmail {#1975
          -message: null
          -headers: Symfony\Component\Mime\Header\Headers {#1978
            -headers: array:3 [
              "to" => array:1 [
                0 => Symfony\Component\Mime\Header\MailboxListHeader {#1971
                  -name: "To"
                  -lineLength: 76
                  -lang: null
                  -charset: "utf-8"
                  -addresses: array:1 [
                    0 => Symfony\Component\Mime\Address {#1958
                      -address: "at@example.com"
                      -name: ""
                    }
                  ]
                }
              ]
              "from" => array:1 [
                0 => Symfony\Component\Mime\Header\MailboxListHeader {#1973
                  -name: "From"
                  -lineLength: 76
                  -lang: null
                  -charset: "utf-8"
                  -addresses: array:1 [
                    0 => Symfony\Component\Mime\Address {#1970
                      -address: "from@example.fr"
                      -name: ""
                    }
                  ]
                }
              ]
              "subject" => array:1 [
                0 => Symfony\Component\Mime\Header\UnstructuredHeader {#1981
                  -name: "Subject"
                  -lineLength: 76
                  -lang: null
                  -charset: "utf-8"
                  -value: "My subject"
                }
              ]
            ]
            -lineLength: 76
          }
          -body: null
          -text: null
          -textCharset: null
          -html: null
          -htmlCharset: null
          -attachments: []
          -cachedBody: null
          -htmlTemplate: "email/email.html.twig"
          -textTemplate: null
          -context: array:1 [
            "data" => array:1 [
              "key" => "value"
            ]
          ]
        }
        */
    }

    public function onMessageSent(SentMessageEvent $event): void
    {
        // Here, the context seems to be empty
        dump($event->getMessage()->getOriginalMessage() );
        // Here is the output, we can see clearly that the context is empty
        // Also the context is empty if I inspect the getEnvelope(), getOriginalMessage() and getMessage() methods
        /*
        MailEventListener.php on line 27:
        Symfony\Bridge\Twig\Mime\TemplatedEmail {#2318
          -message: null
          -headers: Symfony\Component\Mime\Header\Headers {#2319
            -headers: array:3 [
              "to" => array:1 [
                0 => Symfony\Component\Mime\Header\MailboxListHeader {#2321
                  -name: "To"
                  -lineLength: 76
                  -lang: null
                  -charset: "utf-8"
                  -addresses: array:1 [
                    0 => Symfony\Component\Mime\Address {#1958
                      -address: "at@example.com"
                      -name: ""
                    }
                  ]
                }
              ]
              "from" => array:1 [
                0 => Symfony\Component\Mime\Header\MailboxListHeader {#2320
                  -name: "From"
                  -lineLength: 76
                  -lang: null
                  -charset: "utf-8"
                  -addresses: array:1 [
                    0 => Symfony\Component\Mime\Address {#1970
                      -address: "from@example.fr"
                      -name: ""
                    }
                  ]
                }
              ]
              "subject" => array:1 [
                0 => Symfony\Component\Mime\Header\UnstructuredHeader {#2323
                  -name: "Subject"
                  -lineLength: 76
                  -lang: null
                  -charset: "utf-8"
                  -value: "My subject"
                }
              ]
            ]
            -lineLength: 76
          }
          -body: null
          -text: """
            


                Email content: value


            


            """
          -textCharset: "utf-8"
          -html: """
            <p>


                Email content: <b>value</b>


            </p>


            """
          -htmlCharset: "utf-8"
          -attachments: []
          -cachedBody: null
          -htmlTemplate: null
          -textTemplate: null
          -context: []
        }
        */
    }
}

Is there a way to ensure that the context is preserved when capturing the sent email, or does Symfony intentionally clear the context after sending the email?


Solution

  • The context as well as other parameters are reset when the email template is rendred.

    So we cannot get the context after the email is sent.

    https://github.com/symfony/symfony/blob/80db69194176c1b0049b42ab9229484f1b139761/src/Symfony/Bridge/Twig/Mime/TemplatedEmail.php#L91-L96