Mailgun having trouble dealing with attachments PH

2019-05-31 19:32发布

问题:

I'm currently connecting to Mailgun's webhooks so I can get any emails sent through and pass it somewhere else.

I've managed to deal with the subject, from and body inputs of the API, but I'm currently having trouble grabbing any attachments to an email.

Mailgun says that they send through a multipart file, but I have tried to catch it and write out the info into a file, but it comes back as an empty array...

$file = fopen(__DIR__ . '/files.txt','w') or die("Can't open file.");
ob_start();
var_dump($_FILES);
fwrite($file, ob_get_clean()) or die("Can't save to file.");
fclose($file);

Within the Post information, I get an attachments parameter which has such things like a url, content-type, name etc. I have tried to go the the url of each attachment, but I get a login box asking for a username and password and says "Server says: MG API". I have no idea if I need to pass something here so I can retrieve the attachment as a file to pass it on.

If anyone could help me out here, then I would truly appreciate it.

Thank you.

回答1:

Great question,

As you said from the inbound webhook coming from mailgun to your server you're being sent a URL from which to fetch the attachment.

In my case it's https://api.mailgun.net/v2/domains/<<<MYDOMAIN>>>>/messages/WyJjZTL.....SJd/attachments/0

Now if I authenticate to that URL with my username(api) and password, I can fetch the attachement. example:

https://API:PASSWORD@api.mailgun.net/v2/domains/<<<MYDOMAIN>>>>/messages/WyJjZTL.....SJd/attachments/0

The password is simply the API key, that you can find in the control panel on the left. (https://mailgun.com/cp)

hope this works for you as well as its working for me, best regards



标签: php mailgun