Archive | Foreman RSS for this section

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: