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:
- Ann email is sent immediately after a user clicks the “reset password” link or button.
- Personalized data is generated adequately – the name and email address of this user.
- The reset password link in the email is correct and leads to an appropriate webpage.
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.
Learn how you can easily test various user scenarios with a ready to use integration of Kakunin (E2E testing framework) + Mailtrap from our case study with The Software House.
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 email@example.com, 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).
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:
- POST to create a resource
- PATCH to update a resource
- GET to get a resource or list of resources
- DELETE to delete a resource
With Mailtrap API, you are able to run the following commands:
- Create a new inbox in a project, reset the inbox credentials and its email address; receive messages, clean one or all messages in the inbox, mark all messages as read; manage users.
- Create a new project, update, and delete it; manage its users.
- Manage forwarding messages to email addresses.
- Inspect the email body by getting RAW HTML (you can also download it), TEXT, and detailed info about the HTML part, including a list of possible errors.
- Receive message attachments.
- Receive message headers (Bcc is also included in this section, notwithstanding that it is not a regular message header).
- Get a SPAM report and domain blacklisting details.
- Manage CORS domains.
It means that you can perform thorough testing of email workflows, message content, and deliverability issues. The list includes checking:
- Email sending script works.
- Email recipients are correct + Bcc testing (on the advanced plans).
- HTML template doesn’t cause errors.
- Mail merge/dynamic content is replaced properly.
- Appropriate files are attached.
- Important links, such as reset password and account confirmation, work.
- Your message doesn’t trigger a spam filter and your domain is not blacklisted, etc.
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?
Try different requests right in the documentation section, then integrate Mailtrap with your system and start testing!