Rails and Google Apps / Gmail integration

Thinking of using Google Apps for Your Domain? I recently made the switch and thought I’d write up the process.

When Service Sidekick first launched I used a typical, simple mail setup:

  1. Installed Postfix

    $ apt-get install postfix

  2. Setup a few aliases in /etc/aliases

    # users (for now one user to rule them all...)
    support:        zackchandler
    sales:          zackchandler
    ...
  3. Updated the aliases

    $ newaliases
  4. Forwarded email to personal email address

    $ echo 'my-personal-email@gmail.com' > /home/zackchandler/.forward

This worked great after enabling Gmail to send from the servicesidekick.com domain. Life was good.

Then the need to add a few additional email addresses raised it’s ugly head. Couldn’t I just add a new user to the server, add a quick forward file and be done? Actually, in my case, no.

The problem is that I found out that not everyone is smart enough to use Gmail. And unfortunately this is one of the few widely used free email services that allows the sending of email using other email addresses. If I setup the forwards, they couldn’t respond from a Yahoo or Hotmail mail account while spoofing the business domain (as Gmail allows). Darn!

Enter Google’s new service Google Apps for Your Domain which includes Gmail (with a few small differences). Could this be just the thing I was looking for?

  1. I went to www.google.com/a and signed up with the servicesidekick.com domain which was a totally painless process.

  2. I updated my MX records to point to Google’s DNS servers.

  3. (Optional) Within the Google Apps dashboard I customized the url for the email service to mail.servicesidekick.com, then updated the CNAME records to point mail.servicekick.com to Google’s DNS servers.

  4. After about an hour for Google to activate the changes we were live!

At this point we could send and receive email fine on the domain through Google Apps.

But what about emails that the application sends out through ActionMailer? We have welcome emails on signup, error notification through the Exception Notification plugin, etc…

It turns out that simply tweaking the server_settings attributes of ActionMailer is not enough to enable integration with Google Apps. The reason is that the ruby SMTP library that ActionMailer delegates to doesn’t speak TLS (SSL) which is a Gmail requirement.

There are two ways to solve this:

  1. Use msmtp. Basically msmtp can serve as an intermediary between your mail user agent and Gmail because it speaks TLS/SSL. If you’re looking to go this direction check out the excellent Send email with ActionMailer through TLS only SMTP server post at Rails Weenie.

  2. Patch Net::SMTP. Grab the second code patch in the excellent How to send ActionMailer mails to GMail.com post.

I went with the second choice as it doesn’t require extra services to be installed and doesn’t require a separate file (~/.msmtprc) with username and passwords to be located in every user’s home directory.

If you’re going with the patch option:

  1. Create a mailer user in Google Apps.

  2. Drop the code from the How to send ActionMailer mails to GMail.com post into lib/smtp_tls.rb.

  3. Add the following to production.rb.

    # config/environments/production.rb
    require 'smtp_tls'
    ActionMailer::Base.server_settings = {
      :address        => 'smtp.gmail.com',
      :port           => 587,
      :domain         => example.com,
      :authentication => :plain,
      :user_name      => 'mailer@example.com',
      :password       => <mailer's google apps password>
    }
  4. (Optional) Change the Exception Notification plugin settings.

    # config/environment.rb
    ExceptionNotifier.exception_recipients = %w(zackchandler@example.com)   
    ExceptionNotifier.sender_address = 'mailer@example.com'

Now you have hosted business mail free from Google and a Rails app that can send out emails at will. Pretty slick…

Update - 2/5/2007

If you have to run Postfix or another mail transfer agent (MTA) anyway for Monit or something else you might find it easier to adjust your delivery settings to avoid collecting mail locally for your domain. This will force your MTA to forward on any email originating locally to Google Apps.

Postfix example:

/etc/postfix/main.cf (before)

mydestination = example.com, localhost.com, , localhost

/etc/postfix/main.cf (after)

mydestination = localhost.com, , localhost
Comment or question via
FYI: This post was migrated over from another blogging engine. If you encounter any issues please let me know on . Thanks.