1

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.

2

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

#!/bin/sh
# 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:

<?php
putenv("HTTP_HOST=".@$_SERVER["HTTP_HOST"]);
putenv("SCRIPT_NAME=".@$_SERVER["SCRIPT_NAME"]);
putenv("SCRIPT_FILENAME=".@$_SERVER["SCRIPT_FILENAME"]);
putenv("DOCUMENT_ROOT=".@$_SERVER["DOCUMENT_ROOT"]);
putenv("REMOTE_ADDR=".@$_SERVER["REMOTE_ADDR"]);

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
0

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.