Setting Up Mailer Using Devise For Forgot Password

In this tutorial I will show you how to set up the mailer for the forgot password feature in Devise. In the tutorial I will be setting up a Gmail account and I will show you how its done using local environment variables. I will also be using Heroku and Foreman to set up environment variables.

I am using Rails 4 and Devise 3 for this tutorial.

Seting Up Development Environment

First we will set up the development mailer for use on your local machine. In “config/environments/development.rb” you should already have included

config.action_mailer.default_url_options = { :host => 'localhost:3000' }

when you installed devise.

Next you should turn on the option to raise an exception if there is an error when sending an email. You can do this by including

config.action_mailer.raise_delivery_errors = true

in the same file. Next we will add the email delivery method. You should leave the values as they are because I will show you how to set your email information later using local environment variables. Using local environment variables will allow you to hide your email information on open source software sites like Github. In the same file add

config.action_mailer.delivery_method = :smtp

config.action_mailer.smtp_settings = {
address: “”,
port: 587,
domain: ENV[“GMAIL_DOMAIN”],
authentication: “plain”,
enable_starttls_auto: true,
user_name: ENV[“GMAIL_USERNAME”],

Make sure that you leave the values as they are in order to allow you to set the local variables later on.

Setting Up Production Environment

Now you will need to edit the file “config/environments/production.rb”. We will be adding very similar things to this file. First you can add:

config.action_mailer.default_url_options = { :host => '' }

NOTE: You may also need to add this line. When I was setting up the mailer, I did not find this line in any other tutorials. I had the development mailer working, however I could not get heroku to work. I received this error in the heroku logs:

ActionView::Template::Error: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true

I found the answer here: ActionView::Template::Error: Missing host to link to on stackoverflow. If you come across this, then add:

Rails.application.routes.default_url_options[:host] = ''

to your production.rb file.

Next you will have to add in these lines. Once again make sure that you leave the variables as they are.

config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = false
config.action_mailer.default :charset => "utf-8"

config.action_mailer.smtp_settings = {
address: "",
port: 587,
domain: ENV["GMAIL_DOMAIN"],
authentication: "plain",
enable_starttls_auto: true,
user_name: ENV["GMAIL_USERNAME"],

Devise includes its own emails for the mailer. These can be found at “app/views/devise/mailer/”

Adding Local Environment Variables

In this section you will set local variables to your local host and your production site (Heroku).

First create a file in the root directory called “.env”. This is the file we will use to hold our environment variables so that the whole world does not have access to them. Within “.env” you should type:

this time filling in your information for your gmail. The GMAIL_DOMAIN is and you will use your own information for the rest of it. In this case we set the variables inside the file and within the development and production files, it will look for these variables.

Heroku recommends using a gem called Foreman. This will allow us to use our .env file by accessing the local variables before starting the application. We are already using Heroku for production so we should only install Foreman for testing and development. Open the Gemfile and add:

group :development, :test do
gem 'foreman'

Then run bundle install and you will have added the file. Next we will create a file in the root directory called “Procfile”. This file will tell the application which files should be run. Inside the Procfile you should add:

web: bundle exec rails s

Next it is very IMPORTANT that you update your .gitignore file. Failure to update your .gitignore may result in your information becoming public to the world. It is a good idea to add in the Procfile to ensure that Heroku does not receive a R11 – Bad Bind Error.


Now you will need to add the local environment variables to Heroku. This link to Heroku can give you a better idea of how it works. To add the variables to Heroku, make sure you are in the root directory and type:

heroku config:add GMAIL_PASSWORD=password12345

replacing the values with your real information. With Foreman installed, you can now start your application and test out the mailer for the local machine. You can use

foreman start

to start your web application.

This tutorial was made using the following resources:


Tags: , , , ,

18 responses to “Setting Up Mailer Using Devise For Forgot Password”

  1. Shane Deconinck says :

    Thanks, this was very helpful!

  2. tim says :

    What about setting this up using another email provider (not Gmail)?

  3. Daniel Bogart says :

    Yo buddy, great tutorial. However, you use smart quotes in your code for the dev environment, which will cause problems for people copy/pasting this code:

    config.action_mailer.delivery_method = :smtp

    config.action_mailer.smtp_settings = {
    address: “”, <— these quotes should be regular quotes " "
    port: 587,
    domain: ENV["GMAIL_DOMAIN"],
    authentication: “plain”, <— these quotes should be regular quotes " "
    enable_starttls_auto: true,
    user_name: ENV["GMAIL_USERNAME"],
    password: ENV["GMAIL_PASSWORD"]

  4. VINOD KUMAR says :

    Thanks.. It is very helpful..

  5. Louis Hoang says :

    Thanks you very much, you save my day

  6. dwightgnjohnson says :

    Reblogged this on DWIGHT => WEB DEVELOPER and commented:
    Seeing an email arrive in your own inbox FROM YOUR OWN APP AND CODE is an amazing feeling! 🙂
    I keep going back to this article on how to properly set it up in both local and production environments.

    **I used gmail for local but Mandrill for production – just copy over the Mandrill .env keys in place of gmail**

  7. neanderslob says :

    Reblogged this on Neanderslob VS Technology and commented:
    Really cleared up my confusion about sending email in both development and production environments.

  8. neanderslob says :

    A simply outstanding tutorial. Thanks so much sir.

  9. neanderslob says :

    Do you know of any way to set this up to send from aliases set up in a google apps account?

  10. Dalton Boll says :

    Thank you very much for this in depth tutorial! I just implemented it on my site, and I’ll be following your Rails 4 Simple Form/Contact Form tutorial to see if it works.

  11. Tony says :

    Great, thanks mate!

  12. Richard says :

    This worked like a charm for development. Excited to test in production later in the week. Very helpful and well-written. Thank you very much!

  13. Jake Sparling says :

    Great writeup, thank you.

    I was a bit hesitant to include my gmail password in plain text even on my own machine. If you enable 2-factor authentication, you can use an application specific password that you can revoke at any time.

    Also, in the Procfile, I had to specify the port in order to get it to run on Heroku:

    web: bundle exec rails server -p $PORT

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: