Tracking mail sent by PHP
Okay, so you have a server that hosts a variety of PHP applications and you want to have a look at the emails it is sending out to make sure none of them are dreaded form spam. Sounds tough eh? Thankfully there is a little trick we can play on Linux servers to grab and track the emails PHP is sending out.
Under the mail section in php.ini, there is a little variable called sendmail_path. This is where you can specify the sendmail command and any included parameters (useful if you want to have a default from address in your emails). The trick is to simply change this to our own script (executable by apache) that will log the email and forward it through to the recipient.
Unfortunately passing around STDIN can be a little more difficult that it first seems and it was too much for my noob bash skilz. So, I reverted to Perl. One thing I like about Perl is the relatively ease in which you can write to pipes, but I digress. Here is the logger script:
In this case I have my log file at /tmp/sentmail.txt
To use, simply save it in a directory that is accessible by the apache user and chmod +x it to make it executable. Then change the sendmail_path in php.ini to point to the script rather than sendmail. Of course, you should test it by sending yourself an email through the mail( ) function and check the apache error_log for any issues.

