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

sheikhwaqas's avatar

Capturing SMTP Message Id

Hi,

I'm using Amazon SES SMTP service to send outgoing Emails from our application built on Laravel 4.2. I can't seem to figure out a way to capture the Message Id which the SMTP server returns when we call Mailer's send method. That is very important for us to capture so we can have Amazon SES send out a notification to our API Endpoint with the Message Id if that message bounces or has any problems.

Any help would be highly appreciated.

Thanks.

Waqas Ashraf

0 likes
10 replies
sheikhwaqas's avatar

Hi,

Thanks for your reply.

I'm actually not using the SesTransport. I'm using the regular SMTP system of Amazon SES which in turn uses SmtpTransport for SwiftMailer. Is there any way if I can get the underlying instance of SwiftMailer it could return me the Message Id when it sends over to the SMTP?

I know for a fact that Amazon SES via API & SMTP both return a Message Id even though the implementation has not been done in Laravel yet.

Waqas

nirmal743's avatar

Actually, You can get Message-ID in Laravel's Mail method:

$message_id = "";
Mail::send('emails.mailTemplate', array(), function ($message) use (&$message_id) {
    $message_id = $message->getId(); // This is the function.
    $message->to('user2@example.com', 'User2')->subject('Checking MessageID');
});
echo $message_id;  // 9910feafbb7fd7ad64c3b8d82627288b@example.com

This is how I solved my problem.

1 like
sheikhwaqas's avatar

Hi Nirmal,

You are right about the functionality that you have mentioned. But the code snippet that you gave me is returning the SWIFT generated Message Id which is kind of no use to me. I want the Message Id that the SMTP server gives in response when it receives the message and this is I'm sure that my SMTP server is giving a message id in response when it gives the 250 response code right with the code it gives the message id but I've checked the source code for SwiftMailer and Laravel and there's no method I can find that is capturing that message id. If you know of that function that can get me the SMTP Server generated Message Id, that would be very helpful for me.

Thanks for your help.

jab1000's avatar

I am trying to access the "messageID" too that AWS SES returns -- do we have to create a "raw email" instead with code formatted around Amazon's SDK instead to get this messageID?

Thanks!

daylight's avatar

For anyone wanting to do this in Laravel 5.2 here is the way I implemented:

  1. Create new file app/Listeners/LogSentMessage.php

  2. Add your logic to the handle() method

     use Log;
    
     public function handle($message)
     {
          Log::info('MESSAGE ID: ' . $message->message->getId());
     }
    

update the app/Providers/EventServiceProvider.php

    'Illuminate\Mail\Events\MessageSending' => [
        'App\Listeners\LogSentMessage',
    ],
2 likes
Mrsameershaikh's avatar

@daylight it worked for me thank!!. I can see the Message ID in the Laravel Log file. but when I store that message id in an variable and and send it to Mail from Listener through job or without job it is not working. And in log i can see the series of message Id's. here is my code. //Listener use Log; public function handle($message) { $messageId=Log::info('MESSAGE ID: ' . $message->message->getId()); SmtpJob::dispatch($messageId); }

//Smtpjob public $messageId; public function _construct($messageId) { $this->mesageId=$messageId; }

public function handle() { Mail::to('[email protected]')->send(new SmtpMail($this->messageId)); }

//SmtpMail public $messageId public function _construct($messageId) { $this->messageId=$messageId; }

public function builf() { return $this->subject('SMTP response Message ID)->view ('emails.SmtpResponse) }

when i do this in log file multiple message ID getting generated and and nothing is getting stored and no email send. I am new to laravel working as intern kindly help. Sorry for grammar mistake ;)

Bunch-a-devs's avatar

In case anyone stumbles across this old thread, in the Event Listener suggested by @daylight above,

public function handle($event)
{
      $sesMessageId = $event->message
          ->getHeaders()->get('X-SES-Message-ID')->getValue();
}

amankhan's avatar

Tested: Laravel 8

Just to clarify the Event has to be MessageSent. This can be see in

Illuminate\Mail\Transport\SesTransport

The call is made to SES using the sendRawMail(...) and after that SES sends you the message id which is passed to the MessageSent event.

As a side note I had the same issue where I needed to get the message ID of SES. I used the 'ses' mailer and it worked. Not sure the 'smtp' mailer will work since the only place the 'X-SES-Message-ID' is set is in the SesTransport.php file. Someone with more knowledge can shed some light on this. But the 'ses' mailer does work.

iamphonghg's avatar

It’s sad that after 10 years, Laravel still doesn’t have any official doc about this.

Here’s how I retrieve the message ID from SES right after sending an email in Laravel 12. I hope this helps someone who encounters the same issue in the future.

$messageId = '';
        	
Event::listen(function (MessageSent $event) use (&$messageId) {
    $messageId = $event->message->getHeaders()->get('X-SES-Message-ID')->getValue();
});

Mail::to('[email protected]')->send(new TestSesMail(htmlString: $html));

Please or to participate in this conversation.