I'm logging all php mail output using the mail.log setting in php.ini. Rather than log to a file I want to have the log data sent to a script for parsing in real-time to raise alerts.

I have tried: mail.log="|/path/to/script.php"

in the hope that it might work like /etc/aliases, but that doesn't work. Any ideas greatly appreciated :-)

System is Postfix on Debian.


One possible solution would be to use a wrapper for sendmail command, for example you can create a script like this:

# file: sendmail-wrapper-php
# env vars: ${HTTP_HOST}, ${REMOTE_ADDR}, ${SCRIPT_NAME}, ${PWD}, ${UID}

# ....
# check and alert 

#send email.
/usr/sbin/sendmail -t -i $*

and php_set_envs.php to sets some PHP variables to the shell environment:


Now you force all customers to use this wrapper script instead of the original /usr/sbin/sendmail binary. Modify your php.ini and add/change:

sendmail_path = /usr/sbin/sendmail-wrapper-php
auto_prepend_file = /www/php/php_set_envs.php

Make sure it has correct access permissions:

$ chown root /usr/sbin/sendmail-wrapper-php
$ chmod 755 /usr/sbin/sendmail-wrapper-php

I hope this help.

  • I like that. I can modify it to work with Postfix instead of Sendmail, then do any processing I want either in bash in the wrapper, or in php in the set_envs. +1 for the putenv – Peter Nov 13 '14 at 15:30

Use cron to execute a script that scan the mail.log file every minute.

//every minute 
* * * * * * php -f /path/to/script.php

I thing with this solution you have to use a database to remember scanned entries

  • Thanks for the suggestion, but it doesn't really work for this case for two reasons. 1) needs to be real-time, not up to 1 minute delay. 2) don't want to parse the whole of that file each time, just the new entry. I'm really after a way to redirect PHP log writing to a shell script, but might just be impossible. (Could have a file alteration listener running looking for appends to the file, but that's getting really hacky) – Peter Nov 10 '14 at 9:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.