Testing applications and web sites that send email can be difficult. During testing you might generate many email notifications and you don’t want to flood a real mailbox. Or you might not want email from a development system being confused for production email. And especially when using production data during development, you might want to make sure you don’t inadvertently send out spurious emails to actual end users.

The solution is frequently to disable email from the development system, so that no emails accidentally leak out. The obvious downside is that developers cannot then look at the email that would have been sent.

You can change all the email address in the system to send to a common mailbox. And then change the address all back to the correct addresses before deploying to production. That can be error prone and might need multiple mailboxes so you can test that the mail is being directed properly.

Another option is to configure your development server to send all mail, regardless of intended recipient to Mailtrap by Railsware (http://mailtrap.io/). Mailtrap is a free service (for now, anyway) that receives email sent to it and makes it available for viewing in a mailbox you can easily share with others.

Start by signing up for an account at http://mailtrap.io/. After validating your account, you’ll get access to the Mailtrap web site. Log in and create a mailbox. The server information you will need is displayed on the mailbox page.

Mailtrap inbox created

On your server, you need an MTA (Mail Transfer Agent) which handles sending messages between servers. You might already have one installed. If you don’t, a good option is Postfix (http://www.postfix.org/). Install Postfix if necessary. You’ll probably need the Cyrus SASL libraries as well.

On Ubuntu:

sudo apt-get install postfix libsasl2-2

On CentOS:

sudo yum install postfix cyrus-sasl cyrus-sasl-plain

Instructions for configuring Postfix are right on the Inbox page.

Add relayhost configuration to Postfix config (usually at/etc/postfix/main.cf):

relayhost = [mailtrap.io]:2525
smtp_sasl_auth_enable = yes
smtp_sasl_mechanism_filter = plain
smtp_sasl_security_options = noanonymous
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd

Also, You need to create a password file at /etc/postfix/sasl_passwd:

mailtrap.io test-blog-c0642e555fdf9858:e20f4506004bd133

Then rebuild password database and restart postfix:

sudo postmap /etc/postfix/sasl_passwd
sudo /etc/init.d/postfix restart

Now all email sent from that system will go to the mailtrap.io mailbox. You can see the message itself, the headers, including who it was sent from and to, and the raw message.

Mailtrap raw message display

You don’t have to worry that any emails get sent to the real addressee. You can let tests and testers generate notifications to their heart’s content.

You can set up the mailbox to be shared with individual Mailtrap users or any user from your domain.

There is an option to forward all mail or individual emails to another account, so you can see the email in whatever mail client you want (e.g., Microsoft Outlook or Gmail). That is very useful for mail that needs to be rendered correctly, such as those with images and/or attachments.

Another option allows access via a simple web services API, so you can integrate Mailtrap into your automated tests. Via an HTTP GET command you can get a JSON response with the message counts for each inbox, the list of emails from a given inbox with basic header information, or the headers and text of a particular email.

Leave a comment

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