The two events fired by laravel during the process of sending mail (i.e Illuminate\Mail\Events\MessageSending AND Illuminate\Mail\Events\MessageSent) returns the different "Message-ID"
And only the message-id received via MessageSending event does match the message-id on the mail inbox.
Hey there,
Unfortunately we don't support this version anymore. Please check out our support policy on which versions we are currently supporting. Can you please try to upgrade to the latest version and see if your problem persists? If so feel free to reply and we'll try to have a look.
Thanks!
Hey, no luck same issue with the latest version i.e 5.8.10
This is odd since the data attribute is the same one being sent to both these events: https://github.com/laravel/framework/blob/58360ffe7094be96e57c7c2628e3c080222867d0/src/Illuminate/Mail/Mailer.php#L258-L261
Can you show the listener's you're implementing for these events?
Yep you are right, However swift mailer generates the new message-Id at the end of Send method.
and dispatchSentEvent method fetches the fresh instance which is causing the issue.
Which Send method is that?
on swiftmailer/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php line no. 224.
issue can be resolved if we clone the object before the sendSwiftMessage on
framework/src/Illuminate/Mail/Mailer.php
Lines 258 to 261 in 58360ff
and supply cloned object to the dispatchSentEvent event but this may not be the right approach.
All of this code is pretty old. While I do get the issue at hand I wonder if we're missing some gotcha about all this.
ok...just for reference use this on the both the events and check the results
`
public function handle($event)
{
$message = $event->message;
$headers = $message->getHeaders();
Log::info($headers->get('Message-ID'));
}
`
It seems to boil down to an old issue in SwiftMailer: https://github.com/swiftmailer/swiftmailer/commit/04bd51b139b7670dd0ba7013ac86f1dafc840bc2#diff-bceaaa9b500e47defdef9169765dc812
I recently struggled with this issue as I wanted to know which emails gets send and found it very hard to find documentation about Message-ID getting changed when emails are being sent.
So if this can help anyone in the meantime...
I found the easiest way was to add a custom header in the MessageSending event and then extract it again in MessageSent:
public function handle(MessageSending $event)
{
if (!$event->message instanceof \Swift_Message) {
return;
}
$message = $event->message;
// Get unique Message ID
$MessageID = $message->getId();
// Unfortunately MessageID is exchanged after successfull SMTP sending, but ...
// We can set the original value in a custom header, and match against that upon MessageSent event
$message->getHeaders()->addIdHeader('Alternate-Message-ID', $MessageID);
... log message here ...
And then extracted in MessageSent event:
public function handle(MessageSent $event)
{
if (!$event->message instanceof \Swift_Message) {
return;
}
$message = $event->message;
// Get unique Message ID
$MessageID = $message->getId();
// Unfortunately MessageID is exchanged after successfull SMTP sending, but ...
// We have set the original value in a custom header, and can match that against database
$OriginalMessageID = $message->getHeaders()->get('Alternate-Message-ID')->getFieldBody();
... update logentry here ...
The Message ID will be encapsulated in arrows this way, but they can easily be removed as well:
$OriginalMessageID = substr($OriginalMessageID, 1, strlen($OriginalMessageID) - 2);
Quick and dirty, but it works very well.
The same problem with laravel 6.6.1
I'm going to close this as this falls a bit outside the scope of Laravel itself and mostly seems a problem with Swiftmailer itself. If you want to, feel free to send in a PR to make this easier.
To change @swift.generated suffix in Message ID - add next code to your AppServiceProvider->boot() method:
\Swift_DependencyContainer::getInstance()
->register('mime.idgenerator.idright')
->asValue(config('mail.domain'));
This will work for both "inline" sending and sending through queue.
Also you may want to explore this file: vendor/swiftmailer/swiftmailer/lib/dependency_maps/mime_deps.php for some insights about swift mailer configs.
Tested in Laravel 6 and 7. Should work with 5.* too.
Most helpful comment
It seems to boil down to an old issue in SwiftMailer: https://github.com/swiftmailer/swiftmailer/commit/04bd51b139b7670dd0ba7013ac86f1dafc840bc2#diff-bceaaa9b500e47defdef9169765dc812
I recently struggled with this issue as I wanted to know which emails gets send and found it very hard to find documentation about Message-ID getting changed when emails are being sent.
So if this can help anyone in the meantime...
I found the easiest way was to add a custom header in the MessageSending event and then extract it again in MessageSent:
And then extracted in MessageSent event:
The Message ID will be encapsulated in arrows this way, but they can easily be removed as well:
$OriginalMessageID = substr($OriginalMessageID, 1, strlen($OriginalMessageID) - 2);Quick and dirty, but it works very well.