Trying to use GMail SMTP (or some other SMTP authenticated with TLS) with ActiveRecord to send email in Ruby on Rails? Up till now this involved requiring smtp_tls.rb in your environment.

Thankfully, Kyle Maxwell has come up with a neater solution.

Install

It’s a plugin, which you can get with this command in your Rails root…

./script/plugin install http://svn.kylemaxwell.com/rails_plugins/action_mailer_optional_tls/trunk/

Due to continued downtime of Kyle’s site please use the following URl:

./script/plugin install git://github.com/collectiveidea/action_mailer_optional_tls.git

Setup

You can turn TLS authentication on and off as you need in your environment with a new key that is added to the smtp config, tls. To use with gmail, the following should be in your environment.rb


ActionMailer::Base.smtp_settings = {
    :tls => true,
    :address => "smtp.gmail.com",
    :port => "587",
    :domain => "YOURDOMAIN",
    :authentication => :plain,
    :user_name => "GOOGLEUSERNAME",
    :password => "GOOGLEPASSWORD"
  }

And that’s it. Enjoy.