Mailcatcher

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.

http://mailcatcher.me
MailCatcher runs a super simple SMTP server which catches any message sent to it to display in a web interface.

Setup:

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
mailcatcher

You should now get an output like:

Starting MailCatcher
==> smtp://127.0.0.1:1025
==> http://127.0.0.1:1080
*** 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)

config.vm.network "private_network", ip: "192.168.56.101"

Stop mail catcher

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

COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mailcatch 1795 vagrant 7u IPv4 12138 0t0 TCP 127.0.0.1:1025 (LISTEN)

sudo kill 1795

Start mailcatcher allowing host ip

mailcatcher --ip=0.0.0.0

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

You can now view this in your browser on http://192.168.56.101:1080

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]
respawn
exec /usr/bin/env $(which mailcatcher) --ip=0.0.0.0

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

Change

;sendmail_path =

to

sendmail_path = /usr/bin/env catchmail -f example@test.com

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

php_admin_value sendmail_path "/usr/bin/env catchmail -f example@test.com”

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

<?php
$subject = 'Test email';
$message = 'This is a test email from the server';
$headers = 'From: webmaster@example.com' . "\r\n" .
'Reply-To: webmaster@example.com' . "\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 *