STVO's avatar
Level 2

Using mailgun inbound email routing

Hi there,

I'm using mailgun to try out some inbound email routing but I can't seem to get to the attachments.

Mailgun uses a webhook (for example: mywebsite.com/webhook/mailgun) where it submits a post request when receiving mail.

I got this working, so getting to 'sender', 'subject' and so on all works, except for the 'attachments' field. This always seems to be empty. Shouldn't the posted data from a webhook contain a attachment link array?

Does anyone have any experience with this? The mailgun documentation is a bit confusing regarding this matter.

 public function mailgun(Request $request)
    {
        $message = new Message;
    $message->sender = $request->input('sender');
    $message->from = $request->input('from');
    $attachments = $request->input('attachments');
    foreach($attachments as attachment){
    ...
    
        $message->save();

    dd($attachments);

    }


0 likes
1 reply
Robstar's avatar

I believe Mailgun send attachments as a multipart file.

For each attachment there should be a url similar to https://api.mailgun.net/v2/domains/<<<YOUR_DOMAIN>>>>/messages/<<LONG_STRING>>>/attachments/0 that conatins the file. Note you'll need to use your Mailgun API credentials to access this url as it is password protected.

Mailgun allows you to attach custom keys and values to each message. See https://documentation.mailgun.com/en/latest/user_manual.html#attaching-data-to-messages

Why not use a key titled something like attachment-NUMBER with the value being the full path http://site.co.uk/file.png (or say the AWS s3 path).

Have you tried bin.mailgun.net? It's very useful for web hooks which are annoying to debug. See http://blog.mailgun.com/product-update-new-postbin-for-debugging-webhooks/

Please or to participate in this conversation.