Mailtrap using Ruby on Rails, Heroku and Devise.

Reading time ~3 minutes | Written By: M.T Davis

In this tutorial, we’ll be using Mailtrap to impliement the password reset feature in the Devise gem. Mailtrap allows developers to create a fake SMTP server for pre-production testing of an applications email functionality without the risk of spamming actually users or customers.

We will be using Heroku to run Mailtrap for this tutorial because of the easy implementation of the Mailtrap service. However, keep in mind this will be for a development environment even thou we are using Heroku.

Mailtrap

I will assume that you have a development environment setup and an account with Heroku. If not, please visit Heroku to setup an account now.

##Lets Get Started If you have Devise install, you can skip this section. Make sure you have the Devise gem in your gemfile.

1
gem devise

Run the bundle command to install it:

1
bundle install

After you install Devise and it’s been added to your Gemfile, you need to run the Devise generator:

1
rails generate devise:install

Then you want to add Devise to any of your models using the generator:

1
rails generate devise MODEL

Replace “MODEL” with the model name used for the application’s user. Most of the time, it’s just

1
User
or
1
Admin
.

Next you need to set up the default URL option

`config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }`

for the Devise mailer in

1
app\environments\development.rb
file

Lastly, you want to create the Devise views:

1
rails g devise:views

This will create the password reset, confirmation and other Devise views.

Make sure you add

 `before_action :authenticate_user!` 

to the controller that you want to associate user authentication too.

Also make sure that

`config.sign_out_via = :get`

is set correctly in

1
app\config\initializers\devise.rb
. This is the default HTTP method used to sign out a resource. The default is
1
:delete
. Be sure to restart your rails server with
1
rails s
.

##Optional To display the alerts after requesting the password reset, adding the following code to

1
app\layouts\application.html.erb

<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>


place it in between the

1
<body>
and
1
<%= yield%>
tags.

##Setting up Mailtrap on Heroku We will skip deploying an app on Heroku. You can visit Heroku App Deployment to get directions on that. Once you have your app setup, you want to enter this following in the CLI:

1
heroku addons:create mailtrap
This will atttached Mailtrap to your Heroku application.

Next, navigate to your Heroku dashboard and select your app and then Mailtrap by Railsware add on.

Heroku Dashboard

This will bring you to the Mailtrap Dashboard:

Mailtrap Dashboard

Then click on “Demo Inbox”

You will see your credentials. You want to copy and past the code for Ruby on Rails. It should look something like this:

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  :user_name => '748a0f58237051',
  :password => '82612e24f66a8e',
  :address => 'mailtrap.io',
  :domain => 'mailtrap.io',
  :port => '2525',
  :authentication => :cram_md5
}

You want to place this in your

1
app\config\environments\development.rb
file. Make sure that you have

`config.action_mailer.perform_deliveries = true`

in that file as well. Once you have everything setup, you should be able to to reset your password at http://localhost:3000/users/sign_in and click on “Forgot your password?” then go back to the Mailtrap dasboard and see your test email.

##In Conclusion Mailtrap is an easy and quick solution to test email functionality without having to install and configure your own SMTP server. Mailtrap supports the following lanaguages and framworks:

  • Ruby on Rails
  • PHP (Most Frameworks)
  • Python (Django)
  • Node.Js (Nodemailer)
  • Java, Scala
  • Perl (MIME::Lite)
  • C# (Plain C#)

You can setup and share multiple test inboxes for teams within your company. Check out Mailtrap for more information.

There is a lot more that you can do with Mailtrap. I just wanted to show how to get it up and running with the service using Heroku in a development environment. Please add any comments below.