Send Email with Rails

by Daniel Kehoe

Last updated 12 December 2012

Sending email from a Ruby on Rails application. How to configure Rails to send email using Gmail or Mandrill accounts. With an introduction comparing Email Service Providers for application transactional email or mailing lists.

If You Are New to Rails

If you’re new to Rails, see What is Ruby on Rails?, the book Learn Ruby on Rails, and recommendations for a Rails tutorial.

Join RailsApps

What is the RailsApps Project?

This is an article from the RailsApps project. The RailsApps project provides example applications that developers use as starter apps. Hundreds of developers use the apps, report problems as they arise, and propose solutions. Rails changes frequently; each application is known to work and serves as your personal “reference implementation.” Each application is accompanied by a tutorial so there is no mystery code. Support for the project comes from subscribers. Please accept our invitation to join the RailsApps project.

Email Service Providers

In many applications, you’ll need infrastructure for three types of email:

  • company email
  • email sent from the app (“transactional email”)
  • broadcast email for newsletters or announcements

No single vendor is optimal for all three types of email; you likely will use several vendors.

For “company email,” that is, sending individual email to customers or business associates, you’ll probably use Gmail or Google Apps for Business. For a single address, you can set up a single Gmail account to receive and send email from a different address. More likely, you’ll want several email addresses for your company mail. For that, use Google Apps for Business.

Transactional Email

For simple testing of email, it’s easy to use Gmail to send email messages from the application. For deployment, when the application must send dozens or thousands of acknowledgments or invitations, you will need a hosted SMTP relay service (also known as an ESP or “email service provider”). Considerable expertise is required to keep email from being filtered as spam (see MailChimp’s Email Delivery For IT Professionals). Use an ESP to increase reliability of delivery. The best services track deliveries and show how well your email is being delivered.

Many of these services offer a free plan allowing up to 200 emails/day:

I recommend the Mandrill by MailChimp service (please leave a comment below with your opinion or recommendation). The Mandrill transactional email service integrates well with the MailChimp email list manager service. Plus, you can send up to 12,000 emails/month from the service for free.

Sign up for a MailChimp account to get started. After you’ve created your MailChimp account, see the instructions How do I use Mandrill if I already have a MailChimp account?.

Mailing List

In addition to sending transactional email messages, you likely will want to send newsletters or announcements to your entire mailing list. SMTP relay services such as SendGrid often offer rudimentary bulk email services but you may want to consider using a dedicated service for mass emailing. You’ll get features such as management of “unsubscribe” requests and templates to design attractive messages.

For broadcast email, consider using a service such as:

I recommend MailChimp (please leave a comment below with your opinion or recommendation). MailChimp allows you to send up to 12,000 emails/month to list of 2000 or fewer subscribers for free. After you sign up for a MailChimp account, get your API key. Look under “Account” for “API Keys and Authorized Apps.” Note that the Mandrill API key (which you get on the mandrill.com site) is different from the MailChimp API key (which you get on the mailchimp.com site).

Configure Email

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

If you’ve used Rails Composer to create a starter application, the tool has already made the necessary configuration changes. However, you must configure the application for your email account.

Configure Devise for Email

If you are using the Devise confirmable module to send email, modify the file:

config/initializers/devise.rb

to set the config.mailer_sender option for the return email address for messages that Devise sends from the application.

Configure ActionMailer

Rails Composer configures ActionMailer for development in 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 true to allow email to be sent during development
config.action_mailer.perform_deliveries = false
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default :charset => "utf-8"

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

Email messages are visible in the log file so there is no need to send email in development. The configuration above will raise delivery errors in development.

Rails Composer configures ActionMailer for production in 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"

This will set the example application to deliver email in production. The configuration above will not raise delivery errors in production.

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

ActionMailer is configured for testing in the config/environments/test.rb file:

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

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

Use a Gmail account

In production, you should use an email service provider such as Mandrill to increase deliverability for email messages from your app.

Use Gmail for experimenting, if you want to keep things simple.

If you have selected Gmail, Rails Composer will set the file config/environments/production.rb to look like this:

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. See the article Rails Environment Variables to learn how to set local environment variables to keep email account passwords secret.

Use a Mandrill account

Use an SMTP relay service such as Mandrill if you want to increase deliverability for email messages from your application.

If you have selected Mandrill, Rails Composer will set the file config/environments/production.rb to look like this:

config.action_mailer.smtp_settings = {
  :address   => "smtp.mandrillapp.com",
  :port      => 25,
  :user_name => ENV["MANDRILL_USERNAME"],
  :password  => ENV["MANDRILL_API_KEY"]
}

Note that the password will be your Mandrill API key.

You can replace ENV["MANDRILL_USERNAME"] and ENV["MANDRILL_API_KEY"] with your Mandrill username and API key. However, committing the file to a public GitHub repository will expose your secret API key. See the article Rails Environment Variables to learn how to set local environment variables to keep email account passwords secret.

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.

Comments

Is this helpful? Your encouragement fuels the project. Please tweet or add a comment. Couldn't get something to work? For the example apps and tutorials, it's best to open an issue on GitHub so we can help you.

comments powered by Disqus