The Wayback Machine - https://web.archive.org/web/20231128090648/https://github.com/symfony/symfony/issues/52645
Skip to content
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

Symfony Mailer: Message context lost when captured on MessageSent event #52645

Open
ayaou opened this issue Nov 19, 2023 · 1 comment
Open

Symfony Mailer: Message context lost when captured on MessageSent event #52645

ayaou opened this issue Nov 19, 2023 · 1 comment

Comments

@ayaou
Copy link

ayaou commented Nov 19, 2023

Symfony version(s) affected

6.3.8

Description

Capturing emails sent with Symfony 6.3 using an event listener for SentMessageEvent results in an unexpected behavior. The context, set on the email when created with TemplatedEmail, appears to be empty when the onMessageSent method is triggered.

Do not know if this is the expected behavior! this was reproduced while trying to get the actual email content that has been set and try to get it along with the context data

How to reproduce

Below is the relevant code for sending the email:

<?php

namespace App\Service;

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

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

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

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

And here's the relevant part of the 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: "[email protected]"
                      -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: "[email protected]"
                      -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
    {
       // Issue: The context seems to be empty here
        dump($event->getMessage()->getOriginalMessage() );
        // Here is the output, we can see clearly that the context is empty, also the htmlTemplate is set to null
        /*
        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: "[email protected]"
                      -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: "[email protected]"
                      -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: []
        }
        */
    }
}


### Possible Solution

_No response_

### Additional Context

_No response_
@ayaou ayaou added the Bug label Nov 19, 2023
@ayaou ayaou changed the title Message context lost when captured on MessageSent event Symfony Mailer: Message context lost when captured on MessageSent event Nov 19, 2023
@n0rbyt3
Copy link
Contributor

n0rbyt3 commented Nov 19, 2023

see #47993

@xabbuh xabbuh added the Mailer label Nov 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants