Mailgun – Laravel retrieving attachments

so I experimented this for hours. if you are using laravel, how do you download the attachments on the email from mailgun?


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

This is not the most elegant way but it works. I wasn’t able to find something like this. so if you want to download the attachments on mailgun email.

Getting the data from incoming emails


Getting the METADATA JSON, I REPEAT JSON of attachments

SAMPLE METADATA of attachments
'attachments' => '[
                        "url": "", 
                        "content-type": "application/vnd.openxmlformats-officedocument.wordprocessingml.document", 
                        "name": "Plan API end points XXXX.docx", 
                        "size": 53185

Getting the file from the email

STEP 1. json decode first the attachments

$files = json_decode($request->input('attachments'),true);

STEP 2. Install mailgun API using composer. (

Step 3. create a new instance of mailgun and use your API key, not the password.

$mg = new Mailgun('key-xxxxxxxxxxxxxxxxx');
    foreach ($files as $file){
                $fileName = $file['name'];
                $content = $mg->getAttachment($file['url'])->http_response_body;

THAT’S IT, you can now download the $content , put in your cloudstorage, whatever.

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x