This add-on is operated by Railsware Products Studio LLC
Just-in-time email delivery.
Mailtrap by Railsware
Last updated February 24, 2023
Table of Contents
Mailtrap is an Email Delivery Platform that allows customers to manage the email infrastructure in one place. We offer one platform that covers all email-related needs in one place: testing, sending, and in-depth tracking to control how email infrastructure works and performs.
Here, on Heroku, Mailtrap is featured as an add-on for safe and comprehensive email testing. Mailtrap Email Testing is implemented as a dummy SMTP server. It catches all your test emails and displays them in virtual inboxes. With Mailtrap Email Testing, development teams can easily test, view, share transactional and other emails sent from DEVELOPMENT and STAGING environments, without spamming real customers:
- make sure that email sending script acually works
- preview and analyze email content (HTML, text, and raw)
- validate HTML/CSS and get a list of possible errors across popular email clients
- check whether personalization works as designed and headers are correct (on the advanced plans, you can also test Bcc in emails)
- make sure that links work properly
- get spam and domain blocklisting reports
- automate your email testing workflows
- forward your test emails to regular recipients and monitor your production server (on the advanced plans)
- report easily on your progress by sharing your email testing results (on the advanced plans)
- organize your email testing data with inboxes and projects
Provisioning the add-on
Mailtrap can be attached to a Heroku application via the CLI:
$ heroku addons:create mailtrap
-----> Adding mailtrap to sharp-mountain-4005... done, v18 (free)
Api Token
will be available in the app configuration. Using your API token, you can get the credentials used to access the newly provisioned service Mailtrap instance, MAILTRAP_API_TOKEN
. This can be done in the following way.
require 'rest-client'
require 'json'
response = RestClient::Resource.new("https://mailtrap.io/api/v1/inboxes.json?api_token=#{ENV['MAILTRAP_API_TOKEN']}").get
first_inbox = JSON.parse(response)[0]
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:user_name => first_inbox['username'],
:password => first_inbox['password'],
:address => first_inbox['domain'], :domain => first_inbox['domain'],
:port => first_inbox['smtp_ports'][0],
:authentication => :plain
}
For more information, please refer to mailtrap.docs.apiary.io. After installing Mailtrap, the application should be configured to fully integrate with the add-on.
Local setup
After provisioning the add-on, it’s necessary to locally replicate the config vars so your development environment can operate against the service.
Though less portable, it’s also possible to set local environment variables using the
export
command locally.
Use the Heroku Local command-line tool to configure, run, and manage process types specified in your app’s Procfile. Heroku Local reads configuration variables from a .env
file. To view all of your app’s config vars, type heroku config
. Use the following command to add the MAILTRAP values retrieved from heroku config to your .env
file.
$ heroku config -s | grep MAILTRAP >> .env
$ more .env
For more information, see the Heroku Local article.
Using with Rails 3.x-6.x
In config/environments/*.rb
specify ActionMailer defaults:
require 'rubygems' if RUBY_VERSION < '1.9'
require 'rest-client'
require 'json'
response = RestClient.get "https://mailtrap.io/api/v1/inboxes.json?api_token=#{ENV['MAILTRAP_API_TOKEN']}"
first_inbox = JSON.parse(response)[0] # get first inbox
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:user_name => first_inbox['username'],
:password => first_inbox['password'],
:address => first_inbox['domain'],
:domain => first_inbox['domain'],
:port => first_inbox['smtp_ports'][0],
:authentication => :plain
}
Using with Django
Add the following to settings.py
import requests
response = requests.get("https://mailtrap.io/api/v1/inboxes.json?api_token=<MAILTRAP_API_TOKEN>")
credentials = response.json()[0]
EMAIL_HOST = credentials['domain']
EMAIL_HOST_USER = credentials['username']
EMAIL_HOST_PASSWORD = credentials['password']
EMAIL_PORT = credentials['smtp_ports'][0]
EMAIL_USE_TLS = True
Tutorials
To learn more about developing the email sending functionality and testing it with Mailtrap, read the following tutorials:
- Sending emails with Ruby
- Sending emails with Django
- Sending emails with PHP
- Sending emails with Node.js
- Sending emails with Java
Dashboard
The dashboard can be accessed via the CLI:
$ heroku addons:open mailtrap
Opening mailtrap for sharp-mountain-4005…
or by visiting the Heroku Dashboard and selecting the application in question. Select Mailtrap from the Add-ons menu.
Removing the add-on
Mailtrap can be removed via the CLI.
$ heroku addons:destroy mailtrap
-----> Removing mailtrap from sharp-mountain-4005... done, v20 (free)
Support
All Mailtrap support and runtime issues should be submitted via on of the Heroku Support channels . Any non-support related issues or product feedback is welcome at support@mailtrap.io .