phplaravelemaillaravel-5.4

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.


Solution

  • 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;
    });