1. Code
  2. Testing

Introduction to Mailtrap: A Fake SMTP Server for Pre-Production Testing

Final product image
What You'll Be Creating

Mailtrap provides a fake SMTP server for your development team to test, view and share emails sent from the pre-production environments and test with real data without the risk of spamming real customers. It's created by Railsware and for many development tasks, using Mailtrap will be free.

Essentially, you sign up for Mailtrap and send all of your pre-production environment email via your fake Mailtrap SMTP server.

Use MailTrap to capture email from testing development and staging environments

Then, all of your mailz belong to Mailtrap. You can view and debug your email within Mailtrap's friendly GUI.

You can even use Mailtrap to place dumps of your production database with real user emails through tests on your staging server. Your automated tests can run against the real data—sending email via Mailtrap, eliminating the risk that test emails go out to real customer email addresses.

How Much Does Mailtrap Cost?

For small developers or small tasks, Mailtrap is free. For larger efforts, costs vary between $120 and $300 annually:

Mailtrap Pricing

Getting Started With Mailtrap

Signing up is easy. You can even use your Google or GitHub account:

Mailtrap Signup You can sign up via Google or Github

I used my GitHub account and the process was easy:

Authorize Signhub with Github via OAuth

Once confirmed, you'll see your demo inbox in the Mailtrap GUI:

The Mailtrap dashboard with your inboxes

Configuring Mailtrap

Next, I'm going to walk you through setting up Mailtrap within your development environment.

When you click on the Settings icon in the inbox list, you'll see that each Mailtrap inbox has its own SMTP server credentials:

Mailtrap SMTP Server credentials

You can reset these credentials whenever you want. 

Mailtrap offers a variety of configuration examples:

Mailtrap Dropdown selector for configuration options

For simplicity, I'll use the Hello application from our Programming With Yii2 series to configure Mailtrap. If you wish to use the code from there to test Mailtrap, clone the GitHub repository linked to this tutorial.

With Yii, I'm updating the SwiftMailer SMTP settings in config/web.php. Here's the default:

Which I changed with my Mailtrap settings:

Then, I visited http://localhost:8888/hello/user/register to sign up again:

Yii Hello Application Sign Up

Yii sends a confirmation email:

Yii Confirmation Email Announcement

Instantly, the message appears in my Mailtrap inbox. 

Note: This is not to be confused with the Mailtrap account confirmation—it's the Yii Hello app account confirmation email sent by my application.

The default display is what you might see in Gmail or another mail client:

Mailtrap inbox display

But there are many tabs to choose from to debug your application's outbound email. Here's the HTML source:

Mailtrap Message HTML source view

Here's a view of HTML validation against your email:

Mailtrap message Check HTML validator

And here's an analysis of the spam score and blacklisting of your message and server:

Mailtrap message analysis - spam report and blacklist report

Mailtrap is such a powerful way to debug your outbound email message content and markup.

Sharing Inboxes and Messages With Your Team

If your team is larger, you can invite all of your developers to access each mailbox with links:

Mailtrap invite developers into your inboxes

Or, you can automatically forward all messages to their own accounts and invite them via their own Mailtrap accounts:

Mailtrap forwarding and shared users

The Mailtrap API

You can also write automated tests against Mailtrap mailbox content using its API, documented on apiary. In other words, you could run automated scripts against a snapshot of your live production database and verify the content and markup of the messages that would be delivered by your codebase using the Mailtrap API.

The Mailtrap API inbox message view example

Want to Learn More?

Mailtrap's capabilities and debugging features are obviously super useful and affordable. If you'd like to see another demonstration, here's a lightning talk on Mailtrap by Yaroslav Lazor from Railsberry 2012:

It's such a simple product to use and so beneficial that I hope you'll give it a try on your own.

Please post any comments, corrections or additional ideas below. You can browse my other Tuts+ tutorials on my instructor page or follow me on Twitter @reifman.

Related Links

Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.