Introduction to Mailtrap: A Fake SMTP Server for Pre-Production Testing of Application Email
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.
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:
Getting Started With Mailtrap
Signing up is easy. You can even use your Google or GitHub account:
I used my GitHub account and the process was easy:
Once confirmed, you'll see your demo inbox in the Mailtrap GUI:
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:
You can reset these credentials whenever you want.
Mailtrap offers a variety of configuration examples:
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:
'errorHandler' => [ 'errorAction' => 'site/error', ], 'mailer' => [ 'class' => 'yiiswiftmailerMailer', 'viewPath' => '@app/mailer', 'useFileTransport' => false, 'transport' => [ 'class' => 'Swift_SmtpTransport', 'host' => 'your-smtp-host-domain', 'username' => 'your-email-or-username', 'password' => 'your-password', 'port' => '587', 'encryption' => 'tls', ], ], 'log' => [ 'traceLevel' => YII_DEBUG ? 3 : 0,
Which I changed with my Mailtrap settings:
'errorHandler' => [ 'errorAction' => 'site/error', ], 'mailer' => [ 'class' => 'yiiswiftmailerMailer', '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,
Then, I visited http://localhost:8888/hello/user/register to sign up again:
Yii sends a confirmation email:
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:
But there are many tabs to choose from to debug your application's outbound email. Here's the HTML source:
Here's a view of HTML validation against your email:
And here's an analysis of the spam score and blacklisting of your message and server:
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:
Or, you can automatically forward all messages to their own accounts and invite them via their own Mailtrap accounts:
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.
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.
Source: Tuts Plus