Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

PloraX's avatar

Getting message-id from email within Mailable

Hello, is there any way to get the message-id from an email within Laravel (5.4) Mailables?

I currently get the id like this which works fine, but since this doesn't support markdowns, 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 help!

0 likes
1 reply
_GR_'s avatar

Hi.

I just faced the same requirement and solved it using withSwiftMessage method by passing the callback function right you're used in your sample. So the Markdown Mailable class' build() method might look like:

public function build() {
    return $this->markdown('markdown.view')
        ->withSwiftMessage(function($message) use (&$message_id) {
            $message_id = $message->getId();
        });
}

Please or to participate in this conversation.