Quantcast
Channel: Jeroen Smeets» coding
Viewing all articles
Browse latest Browse all 2

Setup Postfix to forward incoming email to PHP

$
0
0

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.


Viewing all articles
Browse latest Browse all 2

Latest Images





Latest Images