[Mailer] fix: use message object from event #47822
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Based on the documentation https://symfony.com/doc/current/mailer.html#messageevent
However right now you can do only the following:
But you can not replace the Message object, even if there is a method
setMessage
on the event.The reason is that the AbstractTransport does get the updated/replaced Envelope object from the event, but not the Message object:
So why would we need to replace the Message object in the first place?
One example would be the signing of messages (see example here https://symfony.com/doc/current/mailer.html#dkim-signer) in a listener. The signer does not modify the existing Message object, but returns a new instance. If we then use
$event->setMessage($signedEmail)
it simply doesn't work, because it's going to be ignored in the transport.See also this comment here, where someone encountered the same issue: #39354 (comment)