hostname与服务器证书不匹配 - 无法发送电子邮件

时间:2012-11-16 01:01:34

标签: ruby-on-rails email pony

我正在使用Pony邮件发送电子邮件(因为我永远无法让ActionMailer在我的本地Windows机器上工作)。

我的user_mailer.rb文件中的代码包含对Pony.mail方法的调用:

Pony.mail({
  :to => email_address, 
  :from => 'MyChairSales <support@mychairsales.com>', 
  :subject => subject, 
  :body => email_body, 
  :html_body => html_body,
  :via => :smtp,
  :via_options => {
    :address              => 'mail.mychairsales.com',
    :port                 => '25',
    :enable_starttls_auto => true,
    :user_name            => 'mychairs',
    :password             => 'thepassword',
    :domain               => "mychairsales.com" # the HELO domain provided by the client to the server
  } 
 })

这是有效的(我已经使用此方法收到了电子邮件),但现在因“主机名与服务器证书不匹配”错误而失败。

这是堆栈跟踪的顶部:

["/usr/lib64/ruby/1.9.3/openssl/ssl-internal.rb:121:in `post_connection_check'",
"/usr/lib64/ruby/1.9.3/net/smtp.rb:585:in `tlsconnect'", "/usr/lib64/ruby/1.9.3
/net/smtp.rb:560:in `do_start'", "/usr/lib64/ruby/1.9.3/net/smtp.rb:519:in `start'", 
"/home4/mychairs/ruby/gems/gems/mail-2.4.4/lib/mail/network/delivery_methods
/smtp.rb:144:in `deliver!'", "/home4/mychairs/ruby/gems/gems/mail-2.4.4/lib
/mail/message.rb:245:in `deliver!'", "/home4/mychairs/ruby/gems/gems/pony-1.4/lib
/pony.rb:166:in `deliver'", "/home4/mychairs/ruby/gems/gems/pony-1.4/lib
/pony.rb:138:in `mail'", "/home4/mychairs/rails_apps/chairsales/app/mailers
/user_mailer.rb:32:in `send_mail'", "/home4/mychairs/rails_apps/chairsales/app/mailers
/user_mailer.rb:23:in `send_password_reset_email'",...

非常感谢任何指导!

1 个答案:

答案 0 :(得分:18)

有点晚了但是我也遇到了这个错误但是使用了Ruby Mail gem。如果您的SMTP服务器支持TLS,它将尝试使用TLS并验证SSL证书。如果为所使用的主机名颁发证书,或者证书无法通过身份验证(例如,如果它是自签名的并且您不信任CA),那么它将失败,并显示错误“hostname不匹配服务器证书“。

要解决此问题,请使用:openssl_verify_mode选项。这可以设置为OpenSSL :: SSL :: VERIFY_NONE以不对证书进行验证 - 它仍将加密SMTP会话。或者OpenSSL库中还有其他可用选项。

使用您的示例,它将是:

Pony.mail({
  :to => email_address, 
  :from => 'MyChairSales <support@mychairsales.com>', 
  :subject => subject, 
  :body => email_body, 
  :html_body => html_body,
  :via => :smtp,
  :via_options => {
    :openssl_verify_mode => OpenSSL::SSL::VERIFY_NONE, 
    :address              => 'mail.mychairsales.com',
    :port                 => '25',
    :enable_starttls_auto => true,
    :user_name            => 'mychairs',
    :password             => 'thepassword',
    :domain               => "mychairsales.com" # the HELO domain provided by the client to the server
  } 
 })

这也适用于Mail gem。