3

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

matfr
  • 109
  • 1
  • 10

1 Answers1

1

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.

regilero
  • 29,806
  • 6
  • 60
  • 99
  • I have now tried to execute using 'nobody' and this is clearly where the error is, as it fails on opening the log file due to permission errors. My log file though, and its directory, and its directory are chmod 0777. What can I do to resolve the error? Thanks for your help. – matfr Jul 18 '11 at 08:04
  • you can try to log on /tmp, the created file are owned by the creator there, at least for debug. For a real long-term solution you'll have to find the rights place with enough (and please no 777) rights on the directory and his parents (need the x right on all parents directory maybe) – regilero Jul 18 '11 at 08:40