Sending Email from Rails Using smtp on Heroku

A recent project we worked on used Ruby on Rails running on Heroku. The actual domain, including all email services, DNS services are hosted elsewhere. In order to get this working, I needed to figure out how to configure Heroku to send email through a remote SMTP server.

First of all configure the production server by adding the following lines to the project /config/environments/production.rb file. If you are going to be testing the SMTP mail functions on your development server you need to add the configuration information to /config/environments/development.rb as well.

  
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  address: "mail.domain.com",
  port: 25,
  authentication: "plain",
  user_name: "user@domain.com",
  password: ENV['SMTP_PASSWORD'],
  enable_starttls_auto: false
}
config.action_mailer.raise_delivery_errors = true

In your configuration replace address: ‘mail.domain.com’ with your own SMTP server hostname, and replace the user_name: “user@domain.com” with the user name for a valid email account on your SMTP server.

Depending on your situation, you may also have different values for port:, authentication: and enable_starttls_auto:. This is what works for a basic SMTP host running plain authentication and no TLS encryption. This configuration is not designed to work with gmail.

Notice that the password to access your SMTP server is not specified here. It is generally a bad idea to store security credentials in a project file if that file is stored in a version control system like git. Anyone with access to that version control system will also get access to the production security information, which could be a big deal for some applications. So in this case the SMTP server password is stored in an environment variable called SMTP_PASSWORD. By setting this variable on the development and production machines, the mail configuration will be complete.

On MAC or Linux development systems, the environment variable can be set one of 2 ways.

Before running the development server (rails server), enter the following command on the command line, substituting the actual password for the word password in quotes.

export SMTP_PASSWORD='password'

Alternatively you can add the same export command to your ~/.bash_profile file.

You can verify that the password has been set by typing:

echo $SMTP_PASSWORD

If you are running foreman as your development rails server, you can have foreman load the environment from a file named .env at the root of your project:

SMTP_PASSWORD='password'

If you are running version control, you should make sure that the .env file is not stored in your repository. If you are using git make sure the following line is in the .gitignore file.

/.env

Once you have the mailer running on your development system using SMTP, you need to run one more command to make it work on heroku.

$ heroku config:add SMTP_PASSWORD='password'

This creates an environment variable on heroku that works the same as environment variables on your development system. At this point you can upload the new version of your project and you will have the ability to send mail using your existing SMTP server.

About Tom Slykhouse

Helping organizations apply technology to improve marketing results.

Connect with me on Google+