Search code examples
ruby-on-railsopenshiftactionmailer

Net::SMTPAuthenticationError (535-5.7.8 Username and Password not accepted.) Openshift production only


I'm trying to send an email through gmail/actionmailer, it works perfectly on my local development box, but when I try and run the same code on production server it gives me an Net::SMTPAuthenticationError (535-5.7.8 Username and Password not accepted.. I think this is because gmail is rejecting my login credentials from an unknown server.But I'm not sure! How do I fix it?

Background:

I am using the figaro gem, and I am setting my environment variables on the openshift server with: rhc set-env. I've checked that the environment variables are set on the server.

The questions here, here, and here are all good, but have not done anything to solve my problem. My problem is a bit different because it only shows up on my server, everything works fine on my local box.

Development:

In development mode, I have the figaro gem installed, and I have my configuration file stored in application.yml which is not checked into git.
Here are it's contents:

## config/application.yml
# Add configuration values here, as shown below.

gmail_username: '[email protected]'
gmail_password: 'PLACE_HOLDER_PASSWORD'

In my controller:

## PLACEHOLDER_controller.rb
 def create
    @placeholder = Placeholder.new(placeholder_params)

    respond_to do |format|
      if @placeholder.save

        puts "SENDING..."
        puts "Sending to: #{ENV['gmail_username']}..."
        PLACEHOLDERMailer.placeholder_email(some, params).deliver         
        puts "SENT!"
    end
  end

Production:

   ## /config/environments/production.rb
  # SMTP                                                                                                                                                                                                           
  config.action_mailer.delivery_method = :smtp
  # SMTP settings for gmail                                                                                                                                                                                        
  config.action_mailer.smtp_settings = {
    :address              => "smtp.gmail.com",
    :port                 => 587,
    :user_name            => "#{ENV['gmail_username']}",
    :password             => "#{ENV['gmail_password']}",
    :authentication       => "plain",
    :enable_starttls_auto => true
  }

Production:

I push the code to my OpenShift repo with git, the server restarts, ostensibly everything is fine. Except that when I create a new placeholder, I get a 500 back from the server.

Here is the log output:

>> rhc tail -a MY_APP
 SENDING...
Sending to: "[email protected]"... ## This is the correct email! It's clearly pulling the proper values from ENV.
INFO -- :   Rendered placeholder_mailer/placeholder_email.text.erb (0.1ms)
INFO -- : 
Sent mail to [email protected] (132.3ms)
INFO -- : Completed 500 Internal Server Error in 224ms
FATAL -- : 
Net::SMTPAuthenticationError (535-5.7.8 Username and Password not accepted. Learn more at
 ):
 app/controllers/placeholders_controller.rb:35:in `block in create'
app/controllers/placeholders_controller.rb:29:in `create'

Which says that the username and password were not accepted. Now, I know that I have the correct username and password. I'm fairly certain that's not the issue. I suspect that (as mentioned in the linked questions) smtp.gmail.com is rejecting my login from an unknown server.

Before anyone suggests it, I have already:

TL;DR

I'm trying to send an email from the server. When I run it on the server it gives me: Net::SMTPAuthenticationError (535-5.7.8 Username and Password not accepted.. I think this is because gmail is rejecting my login credentials from an unknown server. How do I fix it?


Solution

  • It turns out Gmail blocks you temporarily if you try to access it programmatically more than once every ten minutes. This was a rate limiting issue.