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

[5.6] Reorder message customization callbacks #22995

Merged
merged 2 commits into from Feb 2, 2018

Conversation

Alexander--
Copy link
Contributor

Call developer-supplied callbacks in Mailer and Mailable a bit earlier, to allow customization of created message body and attachments.

Body and attachments of SwiftMailer messages are opaque encoded blobs, which makes them hard to customize after creation. This PR ensures that callbacks, registered by withSwiftMessage, are executed at earlier point, before SwiftMailer message emits it's body. This enables setting email encoder (8bit/base64 etc.) and other customization of message body.

The change isn't particularly disrupting, because current implementation does not allow to modify the message at all, aside from setting headers. While it's theoretically possible to interact with message from the callback by breaking encapsulation and/or converting full message to string, a brief look at uses of withSwiftMessage in public Github project and Laravel documentation shows that all of them are limited to fiddling with email headers and adding attachments.

Fixes #22982

@taylorotwell taylorotwell merged commit 740cc61 into laravel:5.6 Feb 2, 2018
@GrahamCampbell GrahamCampbell changed the title Reorder message customization callbacks [5.5] Reorder message customization callbacks Feb 2, 2018
@GrahamCampbell GrahamCampbell changed the title [5.5] Reorder message customization callbacks [5.6] Reorder message customization callbacks Feb 2, 2018
@domanb
Copy link

domanb commented Jan 9, 2019

This change seems to have affected our production code that was previously working great in 5.5. I would send an email, then use the withSwiftMailer callback to get a copy of the message, then use IMAP to copy the sent email to my bosses 'Sent' folder for his email address. Here was the code I was using:

Mail::to($recipient)->send($mail->withSwiftMessage(function($message) use (&$mailString) { $mailString = $message->toString(); }));

After this merge, $mailString just has the headers, no body. How do I go about getting the full email so I can send it off using IMAP?

@Alexander--
Copy link
Contributor Author

@domanb there seem to be several hooks, that allow to intercept complete message being sent. MessageSending looks particularly promising.

Prior to this change there have been no way to modify a message body before it is built.

@domanb
Copy link

domanb commented Jan 11, 2019

Great, thanks Alexander, I'll look into that and see if I can make something work. I put a hacky workaround in place for now, but it's pretty fragile, this looks much more promising. Cheers!

LukeTowers added a commit to octobercms/library that referenced this pull request Oct 5, 2019
Accomplishes this by firing the message callback before content is added to the message.

This fixes an issue where subjects set on the message object through the callback were not able to override a mail template's subject as they're supposed to in System\Classes\MailManager because the callback was called after the MailManager's listener for mailer.beforeAddContent had already fired. The original issue was reported and fixed in octobercms/october#1665. However, that fix was then later broken by f409ad4 which was for the L5.5 upgrade in Build 420. It's also worth noting that Laravel has moved the callback to be before the content is added starting in L5.6, so this change also brings us closer in line with Laravel for the future 6.0 upgrade (see laravel/framework#22995 for that commit and background information).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants