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

ctyler's avatar

Mailgun API sending, logging and updating

Hello, I have set up an application for my company to send mass notification out to employees. I was using SMTP but have switched to the API. Most other applications when sending to an API there is usually a response but I am not sure how to capture that in Laravel. In particular, I am looking for the message id so I can store the log and receive status updates. However, all the send methods of MAIL are void and don't return anything - unless I am missing something.

Here is my code:

 try {
            Mail::to($this->email)
                ->send(new EmailMessage($this->message));
            $status = 'success';
            $status_message = '';
        } catch(\Exception $e) {
            $status = 'failed';
            $status_message = $e->getMessage();
        }

According to the Mailgun documentation when sending over the API there is a response, im just not sure how to capture it:

{
  "message": "Queued. Thank you.",
  "id": "<[email protected]>"
}

0 likes
9 replies
Braunson's avatar

What about setting up webhooks to your app from MailGun and catch the events then?

ctyler's avatar

@Braunson I plan on using the webhook. The problem is when I send the message and insert the record into the database, when an update for a message comes into the webhook URL, how do I determine which message the update is for? I know mailgun assigns a unique ID to every message, but how do I capture that when the message is sent?

Braunson's avatar

@ctyler There's a few ways to do this.

One example is using the Illuminate\Mail\Events\MessageSending event (https://github.com/laravel/framework/blob/8.x/src/Illuminate/Mail/Events/MessageSending.php), then create a listener for that event.

In the listener you can do:

public function __construct(MessageSending $event)
{
    logger()->info('Message ID: ' . $event->message->getId());
    // Record to your DB for reference
}

If you need to pass any data from the mailable like a reference to a contact, you can add it as a header in the Mailable and retrieve the header in the listener or set a public parameter in the Mailable class and you should have access to it from the listener. I used this in a previous project where we had to log emails going out plus append headers to the email and when MG called our webhook we could read the message ID and/or headers to retrieve the record in the database for that specific email to update the status, etc.

Sinnbeck's avatar

What you do is to hook into swift message and get the id https://laravel.com/docs/8.x/mail#customizing-the-swiftmailer-message

I don't recall the exact name but I think it's $message->messageId()

My code is something like this

public function build()
{
    $this->view('emails.orders.shipped');

    $this->withSwiftMessage(function ($message) {
        $this->id = $message->messageId();
    });

    return $this;
}

public function getMessageId()
{
    return $this->id;
}

I then call getMessageId() after sending to get the id

ctyler's avatar

@Sinnbeck I think that gets the the ID from Swift and I know I can send that along with the message and when status updates come in, mailgun will send that back. I can definitely do it that way.

Would you happen to know of a tutorial where that is demonstrated.

Sinnbeck's avatar

@ctyler no I figured it out myself by trial and error. If you cant get it working, I will share my code when I get to my computer

And yes the message id originates from swift, but it works like a charm.

1 like
ctyler's avatar

@sinnbeck I am going to give it a try to see if I can get it. I am just having a hard time testing the webhook because the test application is not accessible from the web.

Sinnbeck's avatar

@ctyler I put mine online using ngrok while testing. It was really easy and ensured everything worked as expected

Sinnbeck's avatar

@ctyler my work flow to get it working was

  1. Get the message id from swift and just log it to a file
  2. Check mailguns logs for the mail, and check if message id matched (it did)
  3. Save message id together with other data regarding the mail in the database
  4. Set up webhooks and have them update the correct record

Please or to participate in this conversation.