user5856424
user5856424

Reputation:

Get message-id with Laravel Mailable

Is there a way to get the message-id of the to be send email within Laravel Mailables?

I currently get the id like this which just works fine, but since this doesn't support markdowns and stuff, I would prefer to get around this:

Mail::send('mail.contact.confirmation', $contactData, function (Message $message) use ($mailTo, $subject, &$headers)
{
    $headers['message-id'] = $message->getSwiftMessage()->getId();
    $message->to($mailTo)->subject($subject);
});

Thanks for any advice.

Upvotes: 1

Views: 6235

Answers (2)

Sameer Shaikh
Sameer Shaikh

Reputation: 1

I had the same challenge where I had to run a Laravel Queue where message ID should be displayed after sending an email. I solved this issue like this.

namespace App\Jobs

inside handle()

$message_Id="";
Mail::send('emails.thanks',array(),function($message) use(&$message_Id){
    $message_Id=$message->getId();
    $message->to('enter email id','any name')->subject('Please check the message ID');
});
echo $message_id;

Upvotes: 0

Chris
Chris

Reputation: 1579

I just had to figure this out on my own, so take my answer with a grain of salt (although it works for me).

Within the "build()" function of a Laravel Mailable, you can do:

$this->withSwiftMessage(function ($swiftmessage) {
   echo $swiftmessage->getId();
});

Hopefully that gets you started. My use-case was editting the ID, so I used:

$this->withSwiftMessage(function ($swiftmessage) use ($newId) {
  $swiftmessage->setId($newId);
  return $swiftmessage;
});

Upvotes: 7

Related Questions