If you have a Rails app that sends e-mails, you are probably using ActionMailer. Unfortunately, starting with Rails 2.2.2, you may have encountered a wonderfully annoying little error that looks like this:

OpenSSL::SSL::SSLError (hostname was not match with the server certificate):
   /usr/lib/ruby/1.8/openssl/ssl.rb:123:in `post_connection_check'
   /usr/lib/ruby/1.8/net/smtp.rb:582:in `tlsconnect'
   /usr/lib/ruby/1.8/net/smtp.rb:562:in `do_start'
   /usr/lib/ruby/1.8/net/smtp.rb:525:in `start'

The are quite a few sites out there that give monkey patches for this problem, but those aren’t very useful because the next time you upgrade Rails, the monkey patch gets obliterated and you’re back to square 1. And then you have to remember how you monkeyed with it the last time to get it working again. Ug…

Instead, fix it the right way!

The reason this problem occurs is that starting with Rails 2.2.2, ActionMailer will automatically enable an encrypted connection with the mail server when trying to send out a message. This “feature” is enabled by default. If you don’t have a fancy, ungodly expensive certificate because you decided to roll your own, or if there is some problem with your certificate or postfix config, you get the above error.

Well, starting with Rails 2.2.3, there is a new option in ActionMailer::Base: enable_starttls_auto. This option allows you to turn off the automatic TLS “feature”.

To use the option, open up your environment.rb and add the following:

# Turn off auto TLS for e-mail
ActionMailer::Base.smtp_settings[:enable_starttls_auto] = false

Restart your app, and the problem is fixed for good. Note that this option will only work if you’re using Ruby 1.8.7 or above. If you’re NOT using 1.8.7 or above, upgrade. It looks like older Ruby versions will not be officially supported by the next versions of Rails.

If you’re afraid of upgrading Ruby, fear not. Just try Ruby Enterprise Edition and Passenger. They’re easy to set up, they have extensive documentation, and you’ll be very pleased with the performance and easy of use!

Happy Holidays!!

Get Scottie Stuff!