Introduction to Mailtrap: A fake SMTP server for pre-production testing of application email

Using Mailtrap is free for most development tasks. It was created by Railsware team. Essentially, you sign up for Mailtrap and send all of your pre-production environment email via your fake Mailtrap SMTP server.

Later on, all of your emails are introduced to Mailtrap SMTP software. You can view and debug your email within Mailtrap’s friendly GUI.
ou 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 emails via Mailtrap, eliminating the risk that test emails go out to real customer email addresses.

How Much Does Mailtrap Cost?

Mailtrap is free for small developers or small tasks. Costs vary between $120 and $300 annually for bigger enterprises.

Let’s Get Started With Mailtrap

You can choose different way to sign up – fill out the registration form or use your Google\Github account:

I used my GitHub account to simplify the process.

After being confirmed, you can find your personal demo inbox:

Mailtrap Settings

Mailtrap can be set up perfectly within your development environment. When you click on the Settings icon in the inbox list, you see that each Mailtrap inbox has its own SMTP server credentials:

You can reset these credentials whenever you want. Various configurations can be found within Mailtrap settings:

For simplicity, I will 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:

'errorHandler' => [ 
    'errorAction' => 'site/error', 
], 

'mailer' => [ 
    'class' => 'yii\swiftmailer\Mailer', 
    'viewPath' => '@app/mailer', 
    'useFileTransport' => false, 
    'transport' => [ 
        'class' => 'Swift_SmtpTransport', 
        'host' => 'mailtrap.io', 
        'username' => '294XXXXXXXXXXdd20', 
        'password' => '403XXXXXXXXXX2f7', 
        'port' => '25', 
        'encryption' => 'tls', 
    ], 
], 

'log' => [ 
    'traceLevel' => YII_DEBUG ? 3 : 0,

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

Yii sends a confirmation email:

The message instantly 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:

However, there are many tabs to choose from to debug your application’s outbound email. Here is the HTML source:

Here is how an HTML against your email looks like:

You can also see an analysis of spam score and blacklisting or your message, which is the aim itself:

After using, I would like to admit that Mailtrap is a truly efficient way to debug your outbound email message content.

How Can You Share Inboxes and Messages with Your Team?

For bigger teams, Mailtrap allows inviting numerous developers to access certain mailbox with links:

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.

This is a guest post by Alex Denisov.

Leave a Reply

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