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.
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
or 1
User
.1
Admin
Next you need to set up the default URL option
for the Devise mailer in
file1
app\environments\development.rb
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
to the controller that you want to associate user authentication too.
Also make sure that
is set correctly in
. This is the default HTTP method used to sign out a resource. The default is 1
app\config\initializers\devise.rb
. Be sure to restart your rails server with 1
:delete
.1
rails s
##Optional
To display the alerts after requesting the password reset, adding the following code to 1
app\layouts\application.html.erb
place it in between the
and 1
<body>
tags.1
<%= yield%>
##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:
This will atttached Mailtrap to your Heroku application.1
heroku addons:create mailtrap
Next, navigate to your Heroku dashboard and select your app and then Mailtrap by Railsware add on.
This will bring you to the 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:
You want to place this in your
file. Make sure that you have1
app\config\environments\development.rb
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.