Mailtrap by Railsware

This add-on is operated by Railsware Products Ltd

Safe email delivery testing for your staging app

Mailtrap by Railsware

Last Updated: 17 February 2015

Table of Contents

Mailtrap is an add-on for providing safe email delivery testing.

Mailtrap is a dummy SMTP server for development teams to test, view, share transactional and other emails sent from DEVELOPMENT and STAGING environments, without spamming real customers

mock SMTP server

Provisioning the add-on

Mailtrap can be attached to a Heroku application via the CLI:

$ heroku addons:add 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 Mailtrap service instance, MAILTRAP_API_TOKEN. This can be done in the following way.

require 'rubygems' if RUBY_VERSION < '1.9'
require 'rest_client'
require 'json'

response = RestClient::Resource.new("https://mailtrap.io/api/v1/inboxes.json?api_token=ENV['MAILTRAP_API_TOKEN']", ssl_version: "TLSv1").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 docs.mailtrap.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 Foreman to configure, run and manage process types specified in your app’s Procfile. Foreman reads configuration variables from an .env file. Use the following command to add the MAILTRAP values retrieved from heroku config to .env.

$ heroku config -s | grep MAILTRAP >> .env
$ more .env

Credentials and other sensitive configuration values should not be committed to source-control. In Git exclude the .env file with "echo .env >> .gitignore" command.

Using with Rails 3.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 Python/Django

Add the following to settings.py

from urllib2 import Request, urlopen
import json
request = Request("https://mailtrap.apiary-mock.com/api/v1/inboxes.json?api_token=<MAILTRAP_API_TOKEN>")
response_body = urlopen(request).read()
credentials = json.loads(response_body)[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

Dashboard

The dashboard can be accessed via the CLI:

$ heroku addons:open mailtrap
Opening mailtrap for sharp-mountain-4005…

or by visiting the Heroku apps web interface and selecting the application in question. Select Mailtrap from the Add-ons menu.

dummy SMTP server. Mailtrap Heroku add-on.

Removing the add-on

Mailtrap can be removed via the CLI.

This will not destroy associated data and can be undone!

$ heroku addons:remove 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.