Net::SMTPAuthenticationError on Sinatra

Background

It is very common to build a contact form for users on each app we build.

As setting up the same SMTP settings each and every time for all the apps we set up an easier solution came to our mind - an independent Sinatra app that only offers an interface to receive a request and email us upon contact request.

Imagine the following flow:

App > Contact Form -> Request -> Sinatra > Mail.deliver -> We receive an email about the new contact


The client always received a 500 Internal Server Error to our request. Internally it looked like this:

Error

Net::SMTPAuthenticationError (Our_IP https://accounts.google.com/ContinueSignIn?plt=somekeysandvalues)

Our smtp settings

  enable :cross_origin
  set :allow_origin, :any
  set :allow_methods, [:get, :post, :options]
  set :allow_credentials, true
  set :allow_headers, settings.allow_headers + ['X-Requested-With']#, ["*", "Content-Type", "Accept", "AUTHORIZATION", "Cache-Control"]
  ActionMailer::Base.delivery_method = :smtp
  if production? # => ENV['RACK_ENV'] == 'production'
    ActionMailer::Base.smtp_settings = {
      :user_name            => ENV['EVIL_CONTACT_SMTP_USER'],
      :password             => ENV['EVIL_CONTACT_SMTP_PASSW'],
      :address              => "smtp.gmail.com",
      :port                 => 587,
      :authentication       => :plain,
      :domain               => 'gmail.com',
      :enable_starttls_auto => true
    }
  else
    ActionMailer::Base.smtp_settings = {
      :address => "localhost",
      :port => '1025'
    }
  end

Solution

Note it's mandatory to change the password. Otherwise it'll not work!

We hope this will help solving all these Net::SMTPAuthenticationError's on Ruby/Sinatra apps using Google SMTP servers.


Credits to Philipp Meißner.
Twitter - @PhilMeissner