Gmail SMTP: Net::SMTPAuthenticationError (530-5.5.1 Authentication Required. Learn more at

Viewed 2469

I am trying to send a notification email when a resume file is uploaded to a job posting. I am trying to use Gmail SMTP in Production but I keep getting these error messages in Heroku logs:

Completed 500 Internal Server Error in 505ms (ActiveRecord: 11.3ms)

Net::SMTPAuthenticationError (530-5.5.1 Authentication Required. Learn more at

Everything works perfectly in Development.rb and I can't figure out why it won't work in production.

Here is my Controller: Please see 'create'

class ResumesController < ApplicationController
  helper_method :count

    def new
    @resume = Resume.new
    @post = Post.find(params[:post_id])
  end

    def create
    @resume = Resume.new(resume_params)
    @resume.user_id = current_user.id
    @resume.post_id = params[:post_id]
    if @resume.save 

      ResumeMailer.resume_received(@resume).deliver_now

      current_user.credits = current_user.credits + 1
      current_user.save!
      flash[:success] = "Congratulations! Your Candidate has been submitted and 1 Credit has been added to your account!"
      redirect_to :root
    end
  end

  def show
    resume = Resume.find(params[:id])
    if current_user.unlocks.where(resume_id: resume.id).length > 0
      send_file resume.document.path.split('?')[0], :filename => resume.document_file_name, :type => ["application/pdf", "application/doc", "application/docx"], :disposition => "attachment"
    elsif current_user.credits > 0
      current_user.credits = current_user.credits - 1
      current_user.save!
      Unlock.create(user_id: current_user.id, resume_id: resume.id)
      send_file resume.document.path.split('?')[0], :filename => resume.document_file_name, :type => ["application/pdf", "application/doc", "application/docx"], :disposition => "attachment"
    else
      flash[:success] = "Your Credit balance is zero. Submit more resumes for more Credits!"
      redirect_to inbox_path
    end
  end

  def inbox
    @resumes = current_user.incoming_resumes.order("created_at DESC")
    end

  def destroy
    @resume = Resume.find(params[:id])
    @resume.destroy
    flash[:success] = "Your Resume has been successfully deleted!"
    redirect_to inbox_path
  end

  def count
    current_user.resumes.count
  end

  private

  def resume_params
      params.require(:resume).permit(:document)
    end

end

Here is my Mailer:

class ResumeMailer < ApplicationMailer
  default from: "MYEMAIL@gmail.com"

  def resume_received(resume)
    @resume = resume
    @post = @resume.post

    mail to: @post.user.email,
            subject: "You have received a new Candidate for the #{@post.title} posting!"
  end
end

Here is my Development.rb SMTP:

  config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }

  config.active_record.raise_in_transactional_callbacks = true

  config.action_mailer.delivery_method = :smtp

  config.action_mailer.smtp_settings = {
    address: "smtp.gmail.com",
    port: 587,
    domain: "gmail.com",
    authentication: "plain",
    enable_starttls_auto: true,
    user_name: ENV["GMAIL_USERNAME"],
    password: ENV["GMAIL_PASSWORD"]
  }

Here is my Production.rb SMTP:

config.action_mailer.default_url_options = { host: 'MYSITE.herokuapp.com', protocol: 'http' }

  # Do not dump schema after migrations.
  config.active_record.dump_schema_after_migration = false

  config.action_mailer.delivery_method = :smtp

  config.action_mailer.smtp_settings = {
    address: "smtp.gmail.com",
    port: 587,
    domain: "gmail.com",
    authentication: "plain",
    enable_starttls_auto: true,
    user_name: ENV["GMAIL_USERNAME"],
    password: ENV["GMAIL_PASSWORD"],
    openssl_verify_mode: 'none'
  }
}

This set up works perfectly fine in Development and I have also already gone into GMAIL settings to allow access to less secure apps. Any help or resources to point me in the right direction would be greatly appreciated.

2 Answers

Though the OP's issue has been solved apparently, I am putting this out for reference of future visitors who encounter a similar problem.

When the error is "Authentication Required" during an attempt to send an email, the first thing to do may be to check if the related config parameters are set exactly as you expect.

Launch the console (if you can) with bin/rails c and run, for example, if Rails 6 (and 5?),

Rails.application.config.action_mailer.smtp_settings[:user_name][-10..-1]
 # => "@gmail.com"  (providing your user_name is at Gmail.)
Rails.application.config.action_mailer.smtp_settings[:password].size > 0
 # => true

Obviously, you can browse them directly if your security policy allows. Note that although you probably specify these pieces of information in an encrypted way in your code, the contents of the Hash above are a plain String.

A potential trap in Rails 6 is that the encrypted strings for your run-time environment may differ from the global one in your Rails application. For example, once you have run bin/rails credentials:edit --environment development, the file config/credentials/development.key (and development.yml.enc) are created even if you add nothing in the credentials, and then your credential items in the development environment will differ from the global one from the moment. So, make sure what are set in your config in your run-time environment are what you intend.

Alternatively, to specify bare plain-text strings for user_name and password in development.rb (or production.rb) temporarily and to run the process to send an email would be a simple experiment, providing the security policy allows you to do so.


As an added note, if the error is "Username and Password not accepted" despite they are correctly set, and if you are using the Gmail server, then chances are it is because of the server setting of the Gmail account used. Specifically, the option "Access for less secure apps" must be turned ON at the Gmail setting.

See this Rails-specific answer, or in more detail, this answer at serverfault about the Gmail-specific issue.

Related