Skip to content

[FrameworkBundle][Mailer] Add a way to configure some email headers from semantic configuration #36736

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

Merged
merged 1 commit into from
Jun 9, 2020

Conversation

fabpot
Copy link
Member

@fabpot fabpot commented May 7, 2020

Q A
Branch? master
Bug fix? no
New feature? yes
Deprecations? no
Tickets n/a
License MIT
Doc PR not yet

The configuration allows to set global sender and recipients, but for the envelope.

If you want to set some global headers, it was not possible (a default from header for instance, of a bcc).

That's implemented in this PR.

@fabpot fabpot force-pushed the mailer-default-headers branch from ed6ee44 to 636b293 Compare May 8, 2020 12:41
Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks :) Here are some things I spotted.

@fabpot fabpot force-pushed the mailer-default-headers branch from 438eb32 to 8a6c1dd Compare June 9, 2020 07:55
@fabpot fabpot force-pushed the mailer-default-headers branch 2 times, most recently from a2851d7 to 7d4cd2f Compare June 9, 2020 08:23
@j-schumann
Copy link

I don't know if commenting here or opening a new issue is better, but this seems the place closest to the action:

I implemented a custom EventSubscriber for the MessageEvent before (currently working in 5.1) but this fails and seems will fail with this PR too in combination with the messenger:

Variant A)
When the messenger is not configured/used to send mails asynchronously the Symfony\Component\Mailer\Mailer::send will directly call the transports send() method: https://github.com/symfony/mailer/blob/master/Mailer.php#L41

Then the transport will dispatch the MessageEvent: https://github.com/symfony/mailer/blob/master/Transport/AbstractTransport.php#L61

Everything works as planned.

Variant B)
The Mailer receives a MessageBus instance and thus will dispatch the MessageEvent himself: https://github.com/symfony/mailer/blob/master/Mailer.php#L47
But because the message and its envelope are cloned before (L48/L49) the listeners cannot modify the message.

The email is than wrapped into a SendEmailMessage which in turn is serialized which eventually calls Symfony\Component\Mime\Message::ensureValidity() which will fail because the message has no FROM header, as the listener could not set it before.

--
The only way now to fix this is to set a (valid) sender address before, check $event->isQueued() in the listener and only overwrite the From header if isQueued == false (when the MessageEvent is triggered by the final transport). This is ugly because there is no sound way to determine if this is a custom sender address or a placeholder which should be replaced by the listener.

I think this PR will fail in the same way: The listener cannot set the headers before the message is serialized (because of the mentioned cloning), thus there will be no From header and serialization will fail. Also the Listener cannot replace the previously set placeholder sender address because it uses 'from' => self::HEADER_SET_IF_EMPTY, as default rule, so it will simply skip the placeholder.

For me the current behavior in 5.1 would qualify as a bug, as "Instead of calling ->from() every time you create a new email, you can create an event subscriber and listen to the MessageEvent event to set the same From email to all messages." like the documentation states is not working because in combination with the messenger the listener cannot modify the message. I can open a new bug report for this if it helps.

Best regards,
Jakob

@fabpot
Copy link
Member Author

fabpot commented Jun 25, 2020

@j-schumann Thanks for the details report. Can you please open a new issue? As the PR here is merged, we won't be able to track your issue properly and it will get lost.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants