Using Google Apps for Email

Posted by greg Fri, 25 Jan 2008 00:46:00 GMT

Alright, I just successfully configured my domain to use google apps for email (phew.. the means one less thing I need to worry about!).

In a nutshell, the process went like this:

  • I created a google apps account for my domain name. Really simple and free!
  • Google needed to verify I owned the name. To do this, you can either copy and paste some code into your site or create a custom CNAME record with a code they give you. I elected to use the CNAME record. They say it takes them around 48hrs. to verify (it only took about 10 minutes, though). Once you’ve created the record be sure and click the button which tells them it’s ready to begin authenticating your site.
  • After that, it was time to move on to my DNS settings. I ended up with these MX records:
    • name | data | auxillary
    • squabl.com. | ASPMX.L.GOOGLE.COM. | 1
    • squabl.com. | ALT1.ASPMX.L.GOOGLE.COM. | 5
    • squabl.com. | ALT2.ASPMX.L.GOOGLE.COM. | 5
    • squabl.com. | ASPMX2.GOOGLEMAIL.COM. | 10
    • squabl.com. | ASPMX3.GOOGLEMAIL.COM. | 10
    • squabl.com. | ASPMX4.GOOGLEMAIL.COM. | 10
    • squabl.com. | ASPMX5.GOOGLEMAIL.COM. | 10
  • Slicehost has a nice tutorial here for help on this.
  • Finally, I ended up with one more CNAME record:
    • name | data | auxillary
    • mail | gns.google.com. | 0
  • Don’t forget to add “.” at the end of all the data entries or you’ll end up with your domain name concatenated to the end of each MX/CNAME record.

  • While those changes were propagating I went back to google apps and enabled webmail. (There was another section where I had to confirm I entered the DNS settings so I could use my own subdomain to access GMail.)

  • Lastly, I created a few users.
  • Yayyyyyyyy!!!!

All this was fine and dandy. I was able to send and receive email directly from GMail using my domain without a problem. Now, I was completely amazed at this point that everything just worked. Now, the last piece to test was sending email directly from a rails app.

What I quickly discovered is my rails app didn’t send email directly out of the box using google apps. In other words, you’ll need to provide a work-around. The root of the problem is outlined here and has to do with the fact Google requires a special handshake using TLS – which isn’t supported by ActionMailer. The patch is to add a file to your lib directory and require it inside your environment files. Unfortunately, the site where the following code originated from is down:

Create the file lib/smtp_tls.rb…

require "openssl"
require "net/smtp"
Net::SMTP.class_eval do
  private
  def do_start(helodomain, user, secret, authtype)
    raise IOError, 'SMTP session already started' if @started
    check_auth_args user, secret, authtype if user or secret

    sock = timeout(@open_timeout) { TCPSocket.open(@address, @port) }
    @socket = Net::InternetMessageIO.new(sock)
    @socket.read_timeout = 60 #@read_timeout

    check_response(critical { recv_response() })
    do_helo(helodomain)

    raise 'openssl library not installed' unless defined?(OpenSSL)
    starttls
    ssl = OpenSSL::SSL::SSLSocket.new(sock)
    ssl.sync_close = true
    ssl.connect
    @socket = Net::InternetMessageIO.new(ssl)
    @socket.read_timeout = 60 #@read_timeout
    do_helo(helodomain)

    authenticate user, secret, authtype if user
    @started = true
  ensure
    unless @started
      # authentication failed, cancel connection.
      @socket.close if not @started and @socket and not @socket.closed?
      @socket = nil
    end
  end

  def do_helo(helodomain)
     begin
      if @esmtp
        ehlo helodomain
      else
        helo helodomain
      end
    rescue Net::ProtocolError
      if @esmtp
        @esmtp = false
        @error_occured = false
        retry
      end
      raise
    end
  end

  def starttls
    getok('STARTTLS')
  end

  def quit
    begin
      getok('QUIT')
    rescue EOFError
    rescue OpenSSL::SSL::SSLError
    end
  end
end

Add these settings to your environment file. (I needed to make a slight change from the original patch I discovered. The original patch had ActionMailer::Base.server_settings. Not sure if this is specific only to config/environments/production.rb because I added my change directly to config/environment.rb and ended up with this…

# config/environments/production.rb
require 'smtp_tls'
ActionMailer::Base.smtp_settings = {
  :address        => 'smtp.gmail.com',
  :port           => 587,
  :domain         => 'example.com',
  :authentication => :plain,
  :user_name      => 'mailer@example.com',
  :password       => ''
}
Comments

Leave a response

Comments