Skip to content

TemplatedEmail are rendered two times #39718

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

Closed
pupaxxo opened this issue Jan 4, 2021 · 0 comments · Fixed by #39733
Closed

TemplatedEmail are rendered two times #39718

pupaxxo opened this issue Jan 4, 2021 · 0 comments · Fixed by #39733

Comments

@pupaxxo
Copy link
Contributor

pupaxxo commented Jan 4, 2021

Symfony version(s) affected: 4.4.18

Description

TemplatedEmail are rendered two times: both the Mailer and the AbstractTransport class dispatch a MessageEvent.
The renderer (generally the BodyRenderer from TwigBridge) do not check if the email is already rendered and override the email contents (re-rendering the twig template).

How to reproduce
Send an email and check the event dispatcher logs, you can use a twig extension with a filter called inside the twig template to easily catch it.
image

Possible Solution
The render should check if the templated email is already rendered? Or maybe dispatch the email only one time?

Additional context

@pupaxxo pupaxxo added the Bug label Jan 4, 2021
derrabus added a commit that referenced this issue Mar 5, 2021
This PR was merged into the 4.4 branch.

Discussion
----------

[TwigBridge] Render email once

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #39718
| License       | MIT
| Doc PR        | -

When `\Symfony\Component\Mailer\Mailer` send an email via the Bus (async) it dispatches an `MessageEvent`, then the consumer call the `\Symfony\Component\Mailer\Transport\AbstractTransport::send` method which also dispatches an `MessageEvent`.

This event is listened by `\Symfony\Bridge\Twig\Mime\BodyRenderer::render` which rendered twice an email.

I'm not sure why the event is send twice, and if we could safely remove one of them (or maybe deprecating the `MessageEvent`, in favor of `SendMessageEvent` + `AsyncMessageEvent`)

This PR store a flag in the Message to avoid rendering it twice.

Commits
-------

186ea59 Render email once
@derrabus derrabus closed this as completed Mar 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants