Found Elsewhere…

Newleaders

Using Gmail SMTP with ActionMailer

Published by Robert Bousquet on 2008-05-22 15:45:00 UTC

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, paperflurrie.com, etc.) with the 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 mail. 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 config/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 config/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 pretty much it. Make sure you enable POP/IMAP access in your Google Account so Rails can send mail via SMTP. It should be noted that the Google’s SMTP send limit is 500 emails per user per day.