Mailtrap Blog

How to Streamline Email Testing with the Mailtrap API

Mailtrap is an online full-featured tool for safe email testing in a pre-production environment. It is implemented as a fake SMTP used for delivering messages to virtual inboxes. You can access its full functionality in two ways: via the web app or API. 

This article features Mailtrap API, from main use cases to the list of commands and brief instructions on how to start using it. 

When should you choose API? 

API is beneficial for automating processes and integrating software systems. 

Use case 1: email sending capabilities

Testing email sending capabilities is one of the most common and simplest use cases. How do you usually verify whether your system actually sends emails to the users? Dummy or disposable email addresses? A test application? This could be an option.

Alternatively, you can integrate your application with Mailtrap API and write a script that will be launching email sending on button clicks (imitate account confirmation, for example). As a result, you will retrieve all sent messages in your Mailtrap inbox and be able to easily compare the number of sent and received emails. 

This way, you can automate and streamline regular testing of email notifications, for example. 

Use case 2: email content verification

There is a more complicated and crucial case: you need to verify email content. Any application that offers account registration has a reset password workflow

You need to test whether:

Automated testing is the most effective and elegant solution in this case. 

The most exciting thing about using Mailtrap here is that it parses your emails. This means that you can inspect each part of the message separately: HTML, text, and raw data. This way, links in the messages are clickable and can be tested with a Selenium script. 

Use case 3: monitor email sending on production or manage incoming data

There is another interesting use case – a “bonus” one. The email per inbox feature allows you to send messages from any external system or service to your Mailtrap inbox. Your Mailtrap inbox gets an email address, something like, that you can specify as “To”, “Cc”, or “Bcc” in any system. 

This way, you can monitor email sending on your production system. 

Let’s say, you are managing a daily deals aggregator that sends daily digests to subscribers. When you use complicated dynamic content based on the user’s location, preferences, and app behavior, it is essential to make sure that your subscribers receive valid messages. 

With Mailtrap API, you can retrieve a copy of such messages, inspect them with a script, and automatically delete them after check-up is completed. 

Also, some Mailtrap users utilize its API not for testing, but for managing incoming data. 

For instance, they receive external billing notifications that contain attachments, and then automatically process those attached invoices (download them to a separate folder, for instance). 

Start Testing

How can you use the Mailtrap API? 

The Mailtrap API uses REST protocol and can return calls in JSON or XML data formats. It is compatible with the majority of common programming languages. 

All you need to use it is your Mailtrap account. Mailtrap supports the following HTTPs requests:

With Mailtrap API, you are able to run the following commands: 

It means that you can perform thorough testing of email workflows, message content, and deliverability issues. The list includes checking: 

With Mailtrap, you also can share your email testing results with your team or clients, by sharing inboxes or whole projects with them or even forwarding them your test emails. 

How do you get started with the Mailtrap API? 

Go to the API section in your Mailtrap account. There you will find your API token and a link to the API documentation. Inspect the documentation – it covers core resources that are used to manipulate the API entities. It also provides detailed examples for each request, for the list of languages and libraries (cURL, Java, Javascript, Node.js, Perl, Python, PHP, Ruby, Go, C#, Visual Basic, Groovy, Objective-C, and Swift). 

Try different requests right in the documentation section, then integrate Mailtrap with your system and start testing!

Exit mobile version