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

trevorpan's avatar

Retrieve messageId from SentMessage in SymfonyMailer in Laravel markdown mail

In Laravel 9 the SwiftMailer was deprecated and I'd like to retrieve the message id for email opens, deletes, etc.

Laravel 8 was like so:

    public function build()
    {
        $email = $this->withSwiftMessage(function (Email $message){
				[
                   'message_id' => $message->getId(),
             ]
        });
....

Laravel 9 (attempt)

  use Symfony\Component\Mailer\SentMessage;
...
        /**
         * Build the message.
         *
         * @return $this
         */
        public function build()
        {
            $email = $this->withSymfonyMessage(function (Email $message){
            });

                $this->publicMessage->mail()->create(
                    [
    						 $mailgunWebhook = self::getSymfonyId($email),
                    ]
                );

            return $this->markdown('emails.public-message-sent')
                ->from('[email protected]', 'Company name')
                ->cc('[email protected]', 'Company name')
                ->subject($this->publicMessage->subject);
        }

// On this function I'm getting a mismatched Message, it receives markdown mailable but wants symfony mailable
        private function getSymfonyId(SentMessage $sentMessage, $email): string
        {
            return $email->message_id = $sentMessage->getMessageId();
        }

I found this, but I'm not doing a custom implementation. https://stackoverflow.com/questions/73632087/how-to-get-email-id-when-using-symfony-mailer

Now, https://laravel.com/docs/9.x/mail#headers shows you can do a custom messageId. Is this functionally equivalent to the SymfonyMailer id? Seems like it'd be easier, however, I'm still thinking grabbing Symfony's id is better as it is already being created.

Any thoughts would be great.

0 likes
3 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

This is how I do it :) In my EventServiceProvider I added an extra listener to the $listen array

\Illuminate\Mail\Events\MessageSent::class => [
            \App\Listeners\LogSentMail::class,
        ],

In the LogSentMail listener ( php artisan make:listener LogSentMail )

public function handle($event)
    {
            /** @var \Illuminate\Mail\SentMessage $message */
        $message = $event->sent;
        $messageId = $message->getSymfonySentMessage()->getMessageId();
        //Do something with the ID
    }

You can access all public properties on the email from $event->data['someProperty']

1 like
trevorpan's avatar

@sinnbeck that is an absolutely fascinating approach!

I was off looking for Symfony's sent message. I should have realized that illuminate is using that. Here's how my implementation works per your guidance.

//class LogSentMail
/**
     * Handle the event.
     *
     * @param  object  $event
     * @return void
     */
    public function handle($event)
    {
    /** @var \Illuminate\Mail\SentMessage $message */
        $message = $event->sent;
        $symfonyMessageId = $message->getSymfonySentMessage()->getMessageId();

        //Retrieve PublicMessage ID
        $publicMessageId = $event->data['publicMessage']->id;

        DB::table('mailgun_webhooks')
            ->where('mailable_id', $publicMessageId)
            ->update(['message_id' => $symfonyMessageId]);
    }

Here's the mail:

       /**
         * Build the message.
         *
         * @return $this
         */
        public function build()
        {
            $email = $this->withSymfonyMessage(function (Email $message){
                $this->publicMessage->mail()->create(
                    [
                        'message_id' => '',
                    ]
                );
            });

Probably dont' event need the empty message_id as it's nullable.

Anyhow, it's also interesting that symfony assigns the random hash to your website domain. So, the old way was like: fe3e77f4b9bca13e185582ada47f65d4@swift.generated but now it's like: fe3e77f4b9bca13e185582ada47f65d4@your-site.com

Thank you!

Please or to participate in this conversation.