0

This is probably pretty simple but I am banging my head against a wall.

I am attempting to parse the data that Mailgun sends via a webhook to my application.

So I set-up a simple script to just test things:-

<?php

if(!empty($_POST))
{
$file = fopen('mail'.time().'.txt','w');
ob_start();
var_dump($_POST);
fwrite($file, ob_get_clean());
fclose($file);
}

?>

Yeah - its ugly I know.

I receive the response and it is written to file no problem.

The issue is that the attachments part of the response is not picked up this way.

The following Django code illustrates what you are meant to do - but I am obviously being either thick or a noob as i can't fathom how to get the PHP equivalent of

def on_incoming_message(request):
 if request.method == 'POST':
     sender    = request.POST.get('sender')
     recipient = request.POST.get('recipient')
     subject   = request.POST.get('subject', '')

     body_plain = request.POST.get('body-plain', '')
     body_without_quotes = request.POST.get('stripped-text', '')

     for key in request.FILES:
         file = request.FILES[key]# note: other MIME headers are also posted here...

     # attachments:
         # do something with the file

 return HttpResponse('OK')

The bit I am being a bit dumb about is

 for key in request.FILES:
     file = request.FILES[key]# note: other MIME headers are also posted here...

How do I access the parts 'outside' of the POST request.

For clarity:-

  1. Email recieved by mailgun and forwarded to myserver.com/mailgun (WORKS)
  2. Parsing the result - can get everything up to 'TOKEN' in list of items Mailgun sends
  3. Postbin DOES show the attachments - so what on earth do I put to get the attachments - what is the equivalent PHP for the Django code above.

Thanks for the help - I am crying at how simple this is and the fact I am stuck but a fresh pair of eyes is desperately needed!

GrahamTheDev
  • 22,724
  • 2
  • 32
  • 64
  • 1
    Here you go: [`$_FILES`](http://php.net/manual/en/reserved.variables.files.php) – knbk Feb 13 '15 at 17:40
  • Oh My - that is even worse that I thought - I got completely bamboozled by the fact it was an API call and got it into my head that I had a problem with the JSON response - thank you for not laughing too hard as you wrote that! – GrahamTheDev Feb 13 '15 at 17:53
  • 1
    Happens to the best of us. I only laughed a bit ;) – knbk Feb 13 '15 at 18:04
  • How do i save the attachments to my server? – Anupal Mar 27 '15 at 09:35

0 Answers0