'OpenSSL::SSL::SSLError: hostname "smtp.gmail.com" does not match the server certificate

This is what i have setup in setup_email.rb file,

if Rails.env.production?
  ActionMailer::Base.smtp_settings = {
      :address              => "smtp.gmail.com",
      :port                 => "587",
      :domain               => "gmail.com",
      :user_name            => "username",
      :password             => "mypass",
      :authentication       => "login",
      :enable_starttls_auto => true
  }
end

This code works in development and staging envoirnment.

This code was working fine on production, suddenly after several days it stopped working and i am getting error message. I have tried setting up openssl_verify_mode: none still does'nt work.

Then i changed gmail account but getting same error. Seems like some server side error

Error:

OpenSSL::SSL::SSLError: hostname "smtp.gmail.com" does not match the server certificate

I have tried different methods out there on internet but its not working. Any help would be appreciated.



Solution 1:[1]

use

openssl_verify_mode: OpenSSL::SSL::VERIFY_NONE

or

openssl_verify_mode: 'none'

Solution 2:[2]

In case this answer shows up for others, I had the same problem with a new Rails 7, Ruby 3.1 app.

In my instance, I send emails from the Rails app to a local Postfix instance which relays email to a commercial provider. Always worked fine for me for earlier apps.

My typical settings are:

 config.action_mailer.smtp_settings = {
    address: "localhost",
    port: 25,
    domain: "WHATEVERYOURDOMAINIS",
    enable_starttls_auto: false,
    openssl_verify_mode: OpenSSL::SSL::VERIFY_NONE
  }

This usually works fine, but with the current app, I kept getting errors like this:

SSL_connect returned=1 errno=0 peeraddr=127.0.0.1:25 state=error: certificate verify failed (Hostname mismatch) (OpenSSL::SSL::SSLError) 

Using the method to test mailers from this helpful post: https://makandracards.com/makandra/52335-actionmailer-how-to-send-a-test-mail-directly-from-the-console I find that using 0 or 'none' instead of OpenSSL::SSL:VERIFY_NONE does nothing, but it pointed me in the direction of the net-protocol gem, which also supports the disable_start_tls option.

I then adjusted my config to:

  config.action_mailer.smtp_settings = {
    address: "localhost",
    port: 25,
    domain: "WHATEVERYOURDOMAINIS",
    openssl_verify_mode: 'none',
    disable_start_tls: true,
  }

This gets rid of the problem in my case, though clearly it's disabling TLS completely so only useful if you're relaying mail like me through a local mailer.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Neeraj Kumar
Solution 2 mezza