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
2) Create a file called
config/initializers/smtp_gmail.rb and paste the following:
= File.open("/config/mailer.yml") mailer_options = YAML.load(mailer_config) ActionMailer::Base.smtp_settings = mailer_options mailer_config
3) Create a
config/mailer.yml file with your Google Hosted Account credentials in it:
--- :address: smtp.gmail.com :port: 587 :user_name: email@example.com :password: s1j234gh : :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(firstname.lastname@example.org) ExceptionNotifier.sender_address = %("John Doe" <email@example.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.