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?

Answers:

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

$request->input('sender')

Getting the METADATA JSON, I REPEAT JSON of attachments
  $request->input('attachments')

SAMPLE METADATA of attachments
'attachments' => '[
                    {
                        "url": "https://se.api.mailgun.net/v3/domains/sandboxcXXXXXXXX19a57487.mailgun.org/messages/XXXXXXXXXXXXXXXXXXX=/attachments/0", 
                        "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. (https://github.com/mailgun/mailgun-php)

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 stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x