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.
Run the bundle command to install it:
After you install Devise and it’s been added to your Gemfile, you need to run the Devise generator:
rails generate devise:install
Then you want to add Devise to any of your models using the generator:
rails generate devise MODEL
Replace “MODEL” with the model name used for the application’s user. Most of the time, it’s just
Next you need to set up the default URL option
for the Devise mailer in
Lastly, you want to create the Devise views:
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
app\config\initializers\devise.rb. This is the default HTTP method used to sign out a resource. The default is
:delete. Be sure to restart your rails server with
To display the alerts after requesting the password reset, adding the following code to
place it in between the
<%= 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:
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.
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
app\config\environments\development.rb file. Make sure that you have
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.