I just ran into a problem configuring a Rails app to deliver email through Google’s SMTP servers. ActionMailer doesn’t support TLS/SSL which is required by Google. Fortunately, the action_mailer_optional_tls plugin provides this functionality.

I wanted to host my SMTP settings in an external YAML file so I wouldn’t end up checking my username and password into the repository. (The YAML file is placed on the production server and a softlink is created during each deploy.) For some reason, I kept getting “connection refused” messages whenever I tried to send email with this configuration:

# smtp.yml
production:
  address: smtp.gmail.com
  port: 587
  tls: true
  domain: foo.com
  authentication: :plain
  user_name: someone@foo.com
  password: secret
 
# production.rb
smtp = YAML::load(File.open("#{RAILS_ROOT}/config/smtp.yml"))
ActionMailer::Base.smtp_settings = smtp[Rails.env]

The problem was the hash returned by YAML. The keys were strings, whereas ActionMailer was expecting the keys to be symbols. The fix was to make the hash use indifferent access:

...
ActionMailer::Base.smtp_settings = smtp[Rails.env].with_indifferent_access

That cleared up the “connection refused” problem. Now my app is sending email like a champ.

  • Digg
  • del.icio.us
  • Facebook
  • Google
  • LinkedIn