0

I'm currently trying to use Mailgun's webhooks to retrieve email, but I'm having a few problems.

My PHP code is currently set up to grab any post data and write it to a txt file so I know that everything is definitely working, but nothing shows up.

Here's my PHP code currently...

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

Simple enough... I then go into my logs on Mailgun and enter the URL to my code and click test. Mailgun tells me that they have successfully sent a sample request, but every time I go into my mail.txt to check, the file is still empty.

I have changed the permissions of the txt file to 655 to find out if it's anything to do with this, but I still get nothing. I have created a Mailgun bin URL and tested the Webhooks on there and all seems to be working.

Is there anything obvious I'm not doing?

CheckeredMichael
  • 571
  • 8
  • 30
  • 1
    Try posting the URL with fake data. –  Oct 06 '14 at 08:13
  • Found out that my PHP file isn't writing any post data to my txt file. I'll take a look as to why, but thank you for the comment. I appreciate it. – CheckeredMichael Oct 06 '14 at 08:26
  • I found this useful link http://stackoverflow.com/questions/13361376/write-output-of-print-r-in-a-txt-file-php and found out that var_dump() won't actually write to a file. Instead I use print_r($_POST, true); – CheckeredMichael Oct 06 '14 at 08:30
  • Although the POST now works in a form, I still get nothing from Mailgun. :( – CheckeredMichael Oct 06 '14 at 08:39
  • Investigating further, I can write to a txt file on my Windows PC, but not on the server which is Linux. Permissions have been set to 655 and 755 for testing, but it still won't write anything to the txt file. – CheckeredMichael Oct 06 '14 at 08:53

1 Answers1

0

I found out the problem was to do with the Linux server not writing the var_dump() function to a text file. I changed the permissions of the folder, created the file myself and changed the permissions, deleted the file to allow for the PHP to create and write to the file, but none of this worked.

My Solution was to use the ob functions which ended up like this...

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

I know this can affect the server in a bad way and be pretty slow. I was only using this for initial testing and has since been removed for production.

CheckeredMichael
  • 571
  • 8
  • 30