Setup Postfix to forward incoming email to PHP

Setting this up took me a couple of minutes — finding the reason why it didn’t work a couple of hours. The question has hundreds of hits on Google, a simple answer hasn’t. Hope this helps someone somewhere you.

Create script to parse incoming mail

<?php
  $data = file_get_contents("php://stdin");
?>

Add a new email address to Postfix configuration

sudo vi /etc/aliases

and add the following line (everything on one line)

email+to+redirect: "| php -q /home/user/full/path/to/your/new/script.php"

Apply the changes to the aliases by running

newaliases

To make sure Postfix will pick up the newly added alias soon. If you’re in a hurry, do a

/etc/init.d/postfix reload

Aaaaaah!

One thing that took some time to figure out: my php script ran ok from the prompt, I had supplied it with test data and I saw all the expected data in my database. Yet, when I triggered the script by sending an email to the new address, Postfix bounced it right back at me, saying

The e-mail system was unable to deliver the message,
but did not report a specific reason. Check the address and try again.
If it still fails, contact your system administrator.

< xxx.yyy #5.0.0 X-Postfix; Command died with status 255: 
   "php -q /home/user/full/path/to/your/new/script.php">

In the end, the reason for this error is simple. It is Postfix telling me PHP stopped executing the script as it found an error. In my case, the error occured when the script ran as user Nobody. When tested from the prompt, I didn’t get the error, as the user had sufficient rights.

Comments

  1. W says:

    “In the end, the reason for this error is simple. It is Postfix telling me PHP stopped executing the script as it found an error. In my case, the error occured when the script ran as user Nobody. When tested from the prompt, I didn’t get the error, as the user had sufficient rights.”

    So what was the command to fix the user rights? I am unable to get it to work.

    • Jeroen says:

      Make sure the php logs to the error log, this will probably tell you about the actual cause of the error. In my case, my php was trying to write to a file and had not the rights to do so. After chmod 777 on the file, the script ran ok.

  2. Arnold says:

    Great post. helped a lot.

    one tip / suggestion.
    look at the server hostname.
    in mij case de hostname was “server”.
    so the postfix aliasses lookd for incomming mail like: php_script@server.yourdomain.com

    i had to create an forwarder (in mysql)
    to redirect php_script@server.yourdomain.com -> php_script@yourdomain.com

  3. Ben says:

    Please do not use “sudo vi /yourfile”. God kills a kity each time you do so.
    Set $EDITOR and use “sudoedit /yourfile” instead.

  4. Aleksi Räsänen says:

    I had a problem with postfix to run my script, because postfix runned my script using username “nobody” and group was “nogroup”.

    I had chmodded my script path & script itself to 775 and my group was set to ‘postfix’ and it failed – no any errors etc. anywhere (at least I had not found them).

    When I changed my script path to be readable/writeable to group nogroup all went fine.

    This problem happened on Debian. I don’t know why scripts are running with nobody/nogroup but anyway now it works. Just for info if anybody have same problem :)

  5. jsd says:

    Thanks for this, very helpful!

  6. tecs says:

    Thanks I got it to work. Appears that I was editing the wrong alias file. I was using the /etc/aliases instead of /etc/postfix/aliases. When I put the php script in both it worked fine. Thanks for this post!

  7. tecs says:

    I have the aliases file setup the same as you have it here and the permissions set correctly, but when i send an email nothing happens. No errors or anything. Do I have to have the script in a certain place? Its just on my desktop right now.

    • jeroensmeets says:

      @tecs, better to give it a safe place. But as long as you point the /etc/aliases line to the file, it should work. So something along the lines of

      email+to+redirect: “| php -q /home/username/Desktop/script.php”

      should work.

  8. jeroensmeets says:

    @Steve — thanks for the link.

  9. Steve Smith says:

    Hey, I’ve been looking for examples to help handle incoming email in PHP and came across this. You may be interested to read my blog post Receiving Incoming Email in Rails on the same subject but for Rails programmers. I know the code is in Rails but the principals of the article should still make sense.

  10. jeroensmeets says:

    @johnny, I chmodded the files to 777 to make sure the script had the necessary rights to read/write them.

  11. johnny says:

    So what was the solution? How did you make postfix run the script with the right privileges??? thanks!!!!

  12. jeroensmeets says:

    Hi hisam, should be only the part before the @, so in your case just his

  13. hisam says:

    what shud i give instead of email+to+redirect: is it his@abc.co or just his. i get error wen i give his@abc.com and do newaliases. pls help!

  14. Va75a77a says:

    Hi,

    I have setup an alias in postfix and it tries to write a file, but it does get a permission denied error, is there a way around this? Thank you for the code above.

  15. Mr.n says:

    Thanks for these info, so helpful…
    Can you publish a php script or a guide on how to read the subject, from, to, body… fields in php and use them inside the script??

Speak Your Mind

*