When doing local development we generally don’t want our test servers sending out mail to the world. And it would be ideal to be able to review the emails our application does send out before deploying the changes to the world. An easy way to achieve this functionality is to override PHP’s sendmail_path config variable. First lets install a few packegs that will allow us to send mail, and some useful scripts to rewrite the mail:
sudo apt-get install procmail sendmail
Next create the following script that will rewrite any mail that all mail generated by PHP’s mail() function to the local user of your choice:
formail -R cc X-original-cc \ -R to X-original-to \ -R bcc X-original-bcc \ -f -A"To: username@localhost" \ | /usr/sbin/sendmail -t -i
Replace username@localhost with your local username or an external email address.
Now update your php.ini file’s sendmail_path:
grep sendmail_path /etc/php5/apache2/conf/php.ini sendmail_path=/usr/local/bin/trapmail
You can then use mail client like mutt or Thunderbird to review the emails, or just tail your mbox file.