【问题标题】:Gmail SMTP: Net::SMTPAuthenticationError (530-5.5.1 Authentication Required. Learn more atGmail SMTP:Net::SMTPAuthenticationError(需要 530-5.5.1 身份验证。了解更多信息,请访问
【发布时间】:2017-12-05 23:10:23
【问题描述】:

我正在尝试在将简历文件上传到职位发布时发送通知电子邮件。我正在尝试在生产中使用 Gmail SMTP,但在 Heroku 日志中不断收到这些错误消息:

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

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

Development.rb 中一切正常,但我不知道为什么它在生产中不起作用。

这是我的控制器:请参阅“创建”

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

这是我的邮件:

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

这是我的 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"]
  }

这是我的 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'
  }
}

此设置在开发中运行良好,我也已经进入 GMAIL 设置以允许访问不太安全的应用程序。任何能指引我正确方向的帮助或资源将不胜感激。

【问题讨论】:

    标签: ruby-on-rails email heroku smtp gmail


    【解决方案1】:

    如何在没有 http 的情况下更改主机(参见下面的示例) 另一个想法是检查您的系统环境访问您的 secret_data 变量是否有效(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'
      }
    

    【讨论】:

    • 感谢您的回复。我在没有 HTTP 的情况下更改了主机,但它仍然无法正常工作。您是否建议我暂时使用我的实际电子邮件凭据只是为了测试一下?我从字面上复制了我在 Development.rb 中的内容并将其粘贴到 Production.rb 中。所以 ENV 变量应该不是问题。
    • 是的,没有秘密变量的改变,我知道它不安全,它只是临时的,只是为了调试
    • 哇哦。它确实奏效了!为什么会这样?我正在使用费加罗宝石。似乎在开发中使用我的秘密变量没有问题。我应该如何在生产中解决这个问题?
    • 好吧,因为你运行 heroku ,我建议这个链接进行更多研究devcenter.heroku.com/articles/config-vars
    • 问题已解决。谢谢widjayd!对于阅读本文的任何人,解决方案是,与其认为我在 Application.yml 中的秘密凭据可以完成工作,我实际上需要使用 run: $ Heroku config:set GMAIL_USERNAME=******* $ Heroku config :set GMAIL_PASSWORD=******
    【解决方案2】:

    虽然OP的问题已经很明显的解决了,我还是把这个放出来以供以后遇到类似问题的访问者参考。

    如果在尝试发送电子邮件过程中出现“需要身份验证”的错误,首先要做的可能是检查相关配置参数是否完全按照您的预期设置。

    使用bin/rails c 启动控制台(如果可以)并运行,例如,如果 Rails 6(和 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
    

    显然,如果您的安全策略允许,您可以直接浏览它们。 请注意,尽管您可能在代码中以加密方式指定这些信息,但上面的 Hash 的内容是纯字符串。

    Rails 6 中的一个潜在陷阱是运行时环境 的加密字符串可能与 Rails 应用程序中的全局字符串不同。例如,一旦你运行了bin/rails credentials:edit --environment development,文件config/credentials/development.key(和development.yml.enc)即使你在凭证中没有添加任何东西,也会创建,然后你在开发环境中的凭证项将从现在开始不同于全球的。因此,请确保您在运行时环境中 config 中设置的内容是您想要的。

    或者,在development.rb(或production.rb)中临时为user_name和password指定纯文本字符串并运行发送电子邮件的过程将是一个简单的实验,提供安全策略允许您这样做。


    作为补充说明,如果错误是“用户名和密码不被接受”,尽管它们设置正确,并且如果您使用的是 Gmail 服务器,那么很可能是因为所使用的 Gmail 帐户的服务器设置。具体来说,必须在Gmail setting 上打开“访问不太安全的应用程序”选项。

    请参阅this Rails-specific answer,或更详细地,请参阅this answer at serverfault,了解有关 Gmail 特定问题的信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-17
      • 2014-01-13
      • 1970-01-01
      • 1970-01-01
      • 2020-02-25
      • 2020-08-19
      • 2012-07-28
      相关资源
      最近更新 更多