For one of our bigger clients, we have a setup with a test server. Their application sends out newsletter and other emails to addresses scattered through the source code. We needed to test some of the features generating emails, but there was no budget to rewrite that code. We decided for a simple solution: We trap all email on the test server and make it available from a web interface.
A test server should not send emails to the outside world. Imagine the surprise of your client, if the web application based newsletter would send test content to all their customers after you copied the production database to the test server to debug a content related error. After this had almost happened to me, I wanted a clean solution for the issue. We could not just completely disable the email server, as we sometimes need to test emails sent by the framework. But nobody was keen on touching the sendmail configuration either.
Together with the hosting partner for that project, the fine guys from Aspectra, we figured out the following simple and nice setup:
They used a tiny script to rewrite the Email to be delivered locally, while preserving the original receipients:
echo ‘formail -R to X-original-to -R cc X-original-cc -R bcc X-original-bcc -f -A “To: localuser” | /usr/sbin/sendmail -t -i' > /usr/local/bin/trapmail
formail is a little tool that is part of the procmail package.
The trapmail script was posted by Sean Coates some while ago. He also posts some code if you need a PHP sendmail implementation.
In the php.ini, the sendmail_path is set to the mail rewriter
sendmail_path = /usr/local/bin/trapmail
Pseudo webmail client for trapped mail
We did want the Client to be able to see the test emails. Instead of setting up a full webmail client somewhere, I wrote some PHP code to read the mbox file and show the emails as they would look when received by the client. Thanks to the PEAR mbox class, this is quite simple. If you are interested, you can download the code here.
Of course, the mailbox must exist on the test server. Also, if your test server is accessible over the internet, you should protect the webmail directory, i.e. using .htaccess.
There is one requirement your application will have to fulfill though: All emails must finally be sent using the standard PHP mail() function. The PEAR mailer class can be configured to use this function. Never use SMTP or any other mail sending possibility, as this would circumvent the email rewriting.