Search code examples
ruby-on-railsemailherokusmtpgmail

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


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: "[email protected]"

  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.


Solution

  • how about change the host without http (see sample below) another idea is also check your system environment access your secret_data variabel whether it's work / not (ENV["GMAIL_PASSWORD"]

      config.action_mailer.default_url_options = { :host => "xxx.xxx.xxx.xxx" }
    
      config.action_mailer.delivery_method=:smtp 
      config.action_mailer.raise_delivery_errors = true
    
      # Gmail SMTP server setup
      ActionMailer::Base.smtp_settings = {
        :address => "smtp.gmail.com",
        :enable_starttls_auto => true,
        :port => 587,
        :authentication => :plain,
        :user_name => "your username",
        :password => 'your pass'
      }