A problem I hit when running a dev environment locally is sending emails.mailcatcherlogo
After messing around with settings to get your box to send emails out and past the spam filters, you then need to be careful anything you run locally doesn’t send emails out to real clients.

To solve this Mailcatcher is the solution I use.
It doesn’t physically send the email out, but to its own local inbox for you to easily view during testing.
MailCatcher runs a super simple SMTP server which catches any message sent to it to display in a web interface.


This example is using ubuntu/trusty32 on a vagrant box

Install mailcatcher

sudo apt-get install build-essential g++ libsqlite3-dev ruby1.9.1-dev
sudo gem install mailcatcher

You should now get an output like:

Starting MailCatcher
==> smtp://
*** MailCatcher runs as a daemon by default. Go to the web interface to quit.

If running from a vagrant box:

Ensure you have a private_network ip set in your Vagrantfile (restart your vagrant box if changed) "private_network", ip: ""

Stop mail catcher

sudo lsof -nP -iTCP:1025 -sTCP:LISTEN

mailcatch 1795 vagrant 7u IPv4 12138 0t0 TCP (LISTEN)

sudo kill 1795

Start mailcatcher allowing host ip

mailcatcher --ip=

Starting MailCatcher
==> smtp://
*** MailCatcher runs as a daemon by default. Go to the web interface to quit.

You can now view this in your browser on

mailcatcherStart on boot:

Save us having to remember to start mail catcher each time we start the box

sudo nano /etc/init/mailcatcher.conf

description "Mailcatcher"
start on runlevel [2345]
stop on runlevel [!2345]
exec /usr/bin/env $(which mailcatcher) --ip=

Sending from PHP:

If you do not already have php installed:

sudo apt-get install apache2 php5 libapache2-mod-php5

Find the location of you php.ini file

php -—ini

In my case it is /etc/php5/cli/php.ini

sudo nano /etc/php5/cli/php.ini


;sendmail_path =


sendmail_path = /usr/bin/env catchmail -f

(You can do the same in your apache config if required)

php_admin_value sendmail_path "/usr/bin/env catchmail -f”

Create a emailtest.php file to send an email out (doesn’t matter where)

$subject = 'Test email';
$message = 'This is a test email from the server';
$headers = 'From:' . "\r\n" .
'Reply-To:' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);

Now you can run this file from the command line to send the email

php emailtest.php

If you see this email in your mailcatcher then everything has been setup correctly.
All emails sent from PHP will now appear in this mailcatcher instead of being sent out into the wild.

Leave a Reply

Your email address will not be published. Required fields are marked *