Search code examples
phppostfix-mtaaliases

Piping with postfix aliases and MySQL


I am trying to get incoming email to a specific email address redirected to a php script. I am using Postfix with address and domain information stored in MySql. As I believe the piping cannot be done from the mysql table, the address is paired within the table with a system alias:

In /etc/aliases:

#Other aliases
php_mail_handler:  "| /usr/bin/php -q /home/mysite/htdocs/mail_handler.php"

This seems to work to a certain extent as /var/log/mail.info contains the following:

Jul 17 14:53:29 mysite postfix/qmgr[21974]: 39F726888003: from=<myaccount@gmail.com>, size=1476, nrcpt=1 (queue active)
Jul 17 14:53:30 mysite postfix/local[21981]: 39F726888003: to=<php_mail_handler@localhost>, orig_to=<php@mysite.com>, relay=local, delay=0.95, delays=0.28/0.01/0/0.66, dsn=2.0.0, status=sent (delivered to command:  /usr/bin/php -q /home/mysite/htdocs/mail_handler.php)

However, the php script never executes (it writes the timestamp and anything on stdin to a log file). The script works when called from the command line or via apache. Its code is as follows:

<?php

$f = fopen('php_handler_log.txt','a+');
$data = file_get_contents("php://stdin");
fwrite($f,date('Y-m-d h:i:s').': '.$data."\n");
fclose($f);

?>

Does anyone have any ideas about why it might not be working or how even to debug it? As no errors are showing-up in the php error log I have specified in the CLI's php.ini.

Many thanks,

Mat


Solution

  • Did you try to execute the script on the command line with user nobody?

    su -c "/usr/bin/php -q /home/mysite/htdocs/mail_handler.php" nobody
    

    Then try to use an existing email address when running the test, you'll maybe get back in the sender email a part of the errors.