Accessing mailgun webhook post array

Published 2 weeks ago by DavidWeller01

What is the best way to filter through this data below. I am trying to access the subject in the header array but with no luck at all. I need to access the headers to cross ref in the database and update it's status. By the way this is only part of the post from Mailgun, i can access everything else except the message-headers

"message": { "headers": { "to": "Mr David Weller [email protected]", "message-id": "", "from": "Ramsbottom Stoves [email protected]", "subject": "Ramsbottom Stoves Survey Confirmation" }, "attachments": [], "size": 2510 },

Any help would be most welcome from a newbie...cheers!


This looks like JSON. How do you get this data? If you get it from the Swift_Message object you have all kind of methods you can call on it

$to = $message->getHeaders()->get('to');

Ah right OK.

This is posted back from the mailgun services to a page of my choosing through a webhook service.

So what i am trying to work out is how do i filter through the JSON data so i can access each item such as message-id and subject.

In laravel i am doing it like this

public function delivered(Request $request) {

    $headers = $request->get('message-headers');

    foreach ($headers as $header => $value){

    echo '<pre>';

        if ($header == 'subject'){
            echo 'subject: '.$value;

    echo '</pre>';


Any ideas Bobby?

Cheers, Dave


What's in the $headers variable? If that is still json you need to decode it using json_decode. Or you should be able to loop over it if it's an array


{ "tags": [], "delivery-status": { "mx-host": "", "attempt-no": 1, "description": "", "session-seconds": 3.1470839977264404, "code": 250, "message": "OK" }, "storage": { "url": "", "key": "eyJwIjpmYWxzZSwiayI6ImRmNDVhYTE4LTg2MTctNGE0NC1iMzE0LTY5MTEwY2RmNjUzYiIsInMiOiJhN2VjOGRjYjVhIiwiYyI6InRhbmtiIn0=" }, "log-level": "info", "id": "Uwgwsef5SnG1JtgC9fH3kg", "campaigns": [], "user-variables": {}, "flags": { "is-routed": false, "is-authenticated": true, "is-system-test": false, "is-test-mode": false }, "recipient-domain": "", "timestamp": 1515418975.594767, "envelope": { "transport": "smtp", "sender": "[email protected]", "sending-ip": "", "targets": "[email protected]" }, "message": { "headers": { "to": "Mrs Katie Barnes [email protected]", "message-id": "", "from": "[email protected]", "subject": "Ramsbottom Stoves Have Published Your Quotation, ref:1082" }, "attachments": [], "size": 5341 }, "recipient": "[email protected]", "event": "delivered" }

Above is the full posted array that is sent to me from the MailGun email service

I have done, json_decode($request->get('message-headers')); and looped through it with foreach() but it still returns the same as above.

I do an if statement to try and find the subject field in the loop but i cannot get this item and it's value. I want to use the subject field to find the same item in the database so i can update it's delivery status.

If you have an example of how you might go about looping through a nested array like the one above i can give it a try. I've looked all over the internet but not found anything that works.

Hope you can help me out here...not much hair left now :(

Please sign in or create an account to participate in this conversation.