Jan. 13, 2009 Update: If you're using Ruby 1.8.7 see Updating Gmail SSL SMTP Support for Ruby 1.8.7 for a compatibility fix to the plugin used in this post.

DreamHost provides it's own SMTP support but their default set-up uses Gmail, which only supports SSL based SMTP connections. Rails 2.2 with Ruby 1.9 provides SSL support in ActionMailer via Ruby's 1.9 Net::SMTP class. That's great but it's still a very new combination and I wanted support for DreamHost's Ruby 1.8.5 and Mephisto using Rails 2.0.2. Fortunately it's simple to get SSL (a.k.a. TLS) based SMTP connections to work. Within a few minutes of hitting the browser search button I saw three options: lift 1.9 Net::SMTP code, use msmtp, and, the method I chose, a small plugin that patches Net::SMTP. No, I didn't look into the merits of each solution and I didn't do any type of exhaustive search to see what else might be available, I just picked what looked like an easy way to get the job done. This post documents where to find the plugin and how to use it.

Monkey patching can be a slippery slope but in this case the solution in Steven Chu's How to use GMail SMTP server to send emails in Rails ActionMailer post was so simple and transparent that I decided it was the way to go. Steven provided the code to extend ActionMailer to support TLS but not the plugin packaging so before doing that myself I did quick search on github and found that Eladio Caritos had the code in an action_mailer_tls git repository.

With Rails 2.1.0 or greater you can install the repository as a plugin:

[mac]$ script/plugin install git://github.com/caritos/action_mailer_tls.git

With older versions you have to install it manually:

[mac]$ git clone --depth 1 git://github.com/caritos/action_mailer_tls.git \
> vendor/plugins/action_mailer_tls
[mac]$ rm -rf vendor/plugins/action_mailer_tls/.git

The README in the plugin is based on Steven's post, which is a bit dated. The recommendation is to add SMTP configuration to config/environment.rb:

34
35
36
37
38
39
40
41
ActionMailer::Base.server_settings = {
  :address => "smtp.gmail.com",
  :port => 587,
  :domain => "mycompany.com",
  :authentication => :plain,
  :user_name => "username",
  :password => "password"
}

Way back in Rails 1.2.2 the config/initializers directory was added to replace this type of use of config/environment.rb and the server_settings method was dropped in favor of smtp_settings. So, I created a config/initializers/mail.rb file that looks like this:

1
2
3
4
5
6
7
8
ActionMailer::Base.smtp_settings = {
  :address => "smtp.gmail.com",
  :port => 587,
  # :domain => "robseaman.com", # HELO domain (not req'd)
  :authentication => :plain,
  :user_name => "blog@robseaman.com",
  :password => "mypass"
}

From an email standpoint that's all there was to it, Gmail support was complete.

Housekeeping

Since I'm using Cached Externals I also added an entry to my config/externals.yml file:

34
35
36
37
38
39
vendor/plugins/action_mailer_tls:
  :type: git
  # DH old git checkout (1.4.4.4) doesn't support -q
  :scm_verbose: true
  :repository: git://github.com/caritos/action_mailer_tls.git
  :revision: 0bc370bbfcbb6c25485c11c13a1662f1728d97e5

and ran:

[mac]$ cap local externals:setup

to cache the plugin. There's very little code in the plugin so I didn't cache it expecting a performance benefit; I just wanted to create an explicit record of where I got it and which revision I used.

Sorry, comments are closed for this article.