Skip to content
Daniel Kehoe edited this page Oct 11, 2012 · 6 revisions

Send Email with Rails

by Daniel Kehoe

Last updated 10 October 2012

Sending email from a Ruby on Rails application. How to configure Rails to send email using Gmail or Mandrill accounts.

This is a guide for developers using the example apps from the Rails Apps repository. Others may find it helpful as well. You can use the Rails Composer tool (“like the ‘rails new’ command on steroids”) to set up email for a starter application.

Configure Email

You must configure your application for your email account if you want your application to send email messages.

Configure ActionMailer

Remove the following from the config/environments/development.rb file:

# Don't care if the mailer can't send
config.action_mailer.raise_delivery_errors = false

Add the following to the config/environments/development.rb file:

# ActionMailer Config
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.delivery_method = :smtp
# change to false to prevent email from being sent during development
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default :charset => "utf-8"

Add the following to the config/environments/production.rb file:

config.action_mailer.default_url_options = { :host => 'example.com' }
# ActionMailer Config
# Setup for production - deliveries, no errors raised
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"

Add the following to the config/environments/test.rb file:

# ActionMailer Config
config.action_mailer.default_url_options = { :host => 'example.com' }

This will set the example application to deliver email in both development and production. It will raise delivery errors in development but not production.

In development, config.action_mailer.default_url_options is set for a host at localhost:3000 which will enable links in Devise confirmation email messages to work properly during development.

For testing, config.action_mailer.default_url_options is set for a host at example.com. Any value allows tests to run.

For production, you’ll need to change the config.action_mailer.default_url_options host option from example.com to your own domain.

Use a Gmail account

If you want to use a Gmail account to send email, you’ll need to modify the files config/environments/development.rb and config/environments/production.rb:

config.action_mailer.smtp_settings = {
  address: "smtp.gmail.com",
  port: 587,
  domain: "example.com",
  authentication: "plain",
  enable_starttls_auto: true,
  user_name: ENV["GMAIL_USERNAME"],
  password: ENV["GMAIL_PASSWORD"]
}

You can replace ENV["GMAIL_USERNAME"] and ENV["GMAIL_PASSWORD"] with your Gmail username and password. However, committing the file to a public GitHub repository will expose your secret password.

If you’re familiar with setting Unix environment variables, it’s advisable to leave config.action_mailer.smtp_settings unchanged and set your environment variables in the file that is read when starting an interactive shell (the ~/.bashrc file for the bash shell). This will keep the password out of your repository.

Are you using a bash shell? Use echo $SHELL to find out. For a bash shell, edit the ~/.bashrc file and add:

export GMAIL_USERNAME="myname@gmail.com"
export GMAIL_PASSWORD="secret*"

Open a new shell or restart your terminal application to continue.

Credits

Daniel Kehoe wrote the article.

Did You Like the Article?

Was this useful to you? Follow rails_apps on Twitter and tweet some praise. I’d love to know you were helped out by the tutorial.

Any issues? Please leave a comment below.

Clone this wiki locally