ActionMailer, DreamHost and “hostname was not match with the server certificate”

Today I wanted to add a simple Suggestion Box to Web of Metal. I planned out about 2 hours of my Sunday since the only unknown that I thought I would have to deal with was setting up the ActionMailer for Dreamhost.

A quick google search and I found a simple post on Dreamhost’s Wiki. So I configured my settings locally and successfully sent the email. These are the settings that I used:

ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address          => 'mail.domain.com',
:port                => 25,
:domain           => 'domain.com',
:user_name        => 'mailer@domain.com',
:password         => 'password',
:authentication   => :login,
}

After adding the settings to my environment.rb I did a quick test and everything worked. “Great!” I said, so now lets move the code to Dreamhost. And that’s were it all went downhill.

The functionality did not work in production. I looked at the logs and found the following error in the log:

HomeController:rescue_action_without_handler:hostname was not match with the server certificate

Ugh. To make a long story short it looks like a problem in Rails 2.2.2 with SSL  on the mailserver. It seems that Rails 2.2.2 automatically turns on something called STARTTLS if your using Ruby 1.8.7 and your SMTP server supports STARTTLS. Well I guess Dreamhost’s SMTP server supports it because it’s definitely turned it on. According to Dan Ryan in this post

“You’ll see this error if your mailserver has an SSL certificate set up for a hostname other than the one to which you are trying to connect. I was experiencing the same error, until I changed my smtp_settings to reflect the hostname of the associated SSL cert.”

This issue is going to be fixed in Rails 2.3 but since I am using 2.2.2, I needed a way to turn it off now. I guess I could of wrote a plugin using the information found in the post above but I found it just as easy to  modify the Rails code. I know, that is a bad practice but I did not feel like wasting any more of my time and my Sunday with this issue. Since Rails is frozen in my vendor directory I just modified vendor/rails/lib/action_mailer/base.rb

def perform_delivery_smtp(mail)
  destinations = mail.destinations
  mail.ready_to_send
  sender = mail['return-path'] || mail.from
  smtp = Net::SMTP.new(smtp_settings[:address], smtp_settings[:port])
  # Comment out the following to fix the "hostname was not match with the server certificate"
  # smtp.enable_starttls_auto if smtp.respond_to?(:enable_starttls_auto)
  smtp.start(smtp_settings[:domain], smtp_settings[:user_name], smtp_settings[:password],
  smtp_settings[:authentication]) do |smtp|
  smtp.sendmail(mail.encoded, sender, destinations)
end

Hope this helps anyone else having the issue.

Leave a Comment