Using MailCatcher for local mail testing

When developing web applications on your local machine, you will often want to test e-mails. For the longest time, I would use an environmental switch in PHP depending on the domain (for example, I would set different values if the domain was stirrell.second-cup-of-coffee or www.second-cup-of-coffee.com) and include values for email addresses. I had my local MAMP really sending out emails and this worked fine. Of course, there is always the chance you’re going to miss an email somewhere and inadvertently send out a real email when you just want to test.

A good solution to this problem is to use a “fake” SMTP server – a program that simulates sending an email and allows you to see what would have been sent – but only exists on your local machine. I had been using one on the Macintosh – MockSMTP which is available on the Mac App Store. It had been working fine for me. I set it up according to the instructions here and it all worked swimmingly… until I upgraded to the newest Mac OS X, Mavericks. I did not receive any errors but I also never received any test emails. Bummer. I sent a tweet to the folks who make MockSMTP but never got a response – the app itself has not been updated since 2011 so I fear that an update might not be coming.

I searched around for an alternative to MockSMTP. Many of the ones I saw were Java-based multi-platform solutions which did not appeal to me. I finally decided to give the free MailCatcher a try. MailCatcher is a Ruby Gem so is installed via the command line and called through the command line. I followed the instructions at their site and it worked straight away.

To get MailCatcher working with PHP, I edited the php.ini configuration files to use catchmail for the sendmail path. In MAMP, you simply go to File > Edit Template > PHP and then choose the version of PHP you are using. You can tell what version of PHP you are using with MAMP by going to the “Server” tab:

PHP version

I added the following line to my configuration file:

sendmail_path = /usr/bin/env catchmail

Update for macOS Sierra – I found that I needed to change this line to:

sendmail_path = /usr/bin/env /usr/local/bin/catchmail -f catcher@mailcatcher.me

Once that is set, all email sent by PHP will go to MailCatcher. You can view mail sent to MailCatcher using a browser to connect to your local machine on port 1080 (http://127.0.0.1:1080/). You will get a simple web interface for viewing the mails that have come in. From then on, simply type “mailcatcher” in the Terminal to start up MailCatcher. This will give you a simple way of testing emails on your local machine without worry of emails getting sent to the client.