Get message-id mail send mailable laravel

  • 03-01-2023
  • 456
  • Laravel 9
  • Haresh Chauhan

Mailable get a message-id after mail successfully send in laravel using the withSwiftMessage method used in the build method, may sometimes developer need a mail message id after a mail is successfully sent to the users for reference purpose.

I stored whole details of the mail sent in the database email table, for knowing the mail sent status and uses notification purpose, after a lot's research I got a solution using withSwiftMessage we will get a reference message id, and this reference id I will store in the email table in the database.

Refer to the below example of how I am getting after mail sending a message id in mailable and then after storing it in the database. apart from the mail message id, I am storing many more details like email, customer_id, email IP, HTML template dynamics that I am sending to the users, etc, etc...


namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQu eue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class WelcomeMail extends Mailable
     use Queueable, SerializesModels;

     public $data;

     * Create a new message instance.
     * @return void
     public function __construct($input)
          $this->data = $input;

     * Build the message.
     * @return $this
     public function build()
          return $this->view('mail.welcome')
          ->subject('Welcome to Webappfix - Verify Email ID.')
          ->withSwiftMessage(function ($swiftmessage){
                    'email_date' => now(),
                    'email_ip' => request()->ip(),
                    'email_for' => 0,
                    'customer_id' => 1,
                    'customer_email' => $this->data['email'],
                    'ref_id' => $swiftmessage->getId(), //MESSAGE ID
                    'email_html' => view('mail.welcome',['data' => $this->data])->render()

We always thanks to you for reading our blogs.


Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us


Haresh Chauhan


We Are Also Recommending You :