A few times in my career, I’ve had the need to test emailing in one form or another. I’ve also had the unfortunate experience of accidentally sending a test email to a real person. As you can imagine, this may cause confusion, embarrassment, lectures, and possibly the death of a small pixie or two. Fortunately, there IS a preventative measure you can take.
When it comes to mail() (as well as many other things), PHP prefers to delegate the heavy lifting to another piece of software: sendmail (or a sendmail compatible command-line mail transport agent). By default, PHP will call your sendmail binary, and pass it the entire message, after composing it from the headers and body supplied by the developer.
One of the side-benefits to this system is the ability to override PHP’s default, and seamlessly hook in your own sendmail-esque binary or script. My favorite parts of this hack, is that it doesn’t require a recompile of PHP or anything all too complicated, just a small change to your php.ini file.
So, without further delay, the magic:
In your php.ini file, change your sendmail path:
sendmail_path=/usr/local/bin/trapmail
And here’s what the script should have in it:
you@devserver:~$ cat /usr/local/bin/trapmail
formail -R cc X-original-cc \
-R to X-original-to \
-R bcc X-original-bcc \
-f -A"To: devteam@example.com" \
| /usr/sbin/sendmail -t -i
And what does this do? It traps all mail that would normally go OUT (say, to a customer), and instead, delivers it to devteam@example.com (with the original fields renamed for debugging purposes). Disaster avoided and pixies are forever in your debt!
Edit: Dont forget to make your trapmail script executable!
