How to get email message id within Mailable in Laravel

Laravel Mailables gives us a huge advantage for sending Markdown template messages.

But some times we need to grab the email message id for future tracking, like for webhooks. Here are the code sample of the mailable class’ build() function grabbing the email message id:

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

@sources:
https://laravel.com/docs/8.x/mail#markdown-mailables
https://laracasts.com/discuss/channels/laravel/getting-message-id-from-email-within-mailable?reply=731448

Leave a Reply