Articles & Support

Back to Discussions

Using Gmail SMTP with ActionMailer

I am big fan of Google Hosted Applications which provide free email accounts, calendars, and document storage for users of your custom domain. I run at least 20 different domains in this manner (robertbousquet.com, newleaders.com, signalfire.com, etc.), with my favorite feature being the email hosting.

I also run about 30 rails applications across 10 servers or so, and I often need to send email from my Rails applications for things like a friendly “Thank your for signing up” notice, or Exception Notifications when something goes wrong. Rather than fiddle with Postfix or sendmail on each of my servers, I use Google’s SMTP servers (provided by the Google Hosted Applications account) to send email. This allows me to skip the confusing Postfix installation/configuration steps, and provides a higher likelihood that my emails will not get caught in spam filters.

ActionMailer, by default, cannot securely connect to Google’s secure SMTP servers, so you have to use the following recipe to enable secure SMTP connections for your Rails application:

1) Download this plugin into your vendor/plugins directory:

action_mailer_tls

2) Create a file called conf/initializers/smtp_gmail.rb and paste the following:

require "smtp_tls"
mailer_config = File.open("#{RAILS_ROOT}/config/mailer.yml")
mailer_options = YAML.load(mailer_config)
ActionMailer::Base.smtp_settings = mailer_options

3) Create a conf/mailer.yml file with your Google Hosted Account credentials in it:

---
  :address: smtp.gmail.com
  :port: 587
  :user_name: john@doe.com 
  :password: s1j234gh
  :authentication: :plain

4) If you’re using the ExceptionNotification plugin, make sure the sender of the notifications matches your account email address, or Google will not send it via SMTP:

ExceptionNotifier.exception_recipients = %w(devteam@doe.com)
ExceptionNotifier.sender_address = %("John Doe" <john@doe.com>)

And that’s it. Your application should be able to send emails through your Google Hosted Account. It should be noted that the Google’s SMTP send limit is 500 emails per user, per day.

Posted on May 25, 2008 by Robert Bousquet

Post Your Thoughts

NOTE: We review each comment added to our site. We do not appreciate unsolicited advertising, inappropriate or offensive comments published to The Leader Board. Challenging questions or opinions are okay. Please be respectful. Thank you.