Action Mailer返回SMTPAuthentcationError

时间:2017-09-28 06:07:48

标签: ruby-on-rails ruby actionmailer

每次尝试通过rails'localhost'或'c9'发送电子邮件时,我都会遇到链接中显示的错误。我在谷歌帐户上启用了“安全性较低的应用程序”并尝试了所有指南,但错误仍然相同。寻求帮助的人。 error snapshot

源文件如下: suggestion_mailer.rb

  class SuggestionMailer < ApplicationMailer

  # Subject can be set in your I18n file at config/locales/en.yml
  # with the following lookup:
  #
  #   en.suggestion_mailer.new_suggestion.subject
  #
  def new_suggestion()
   #@suggestion = suggestion


  mail( :from => "xxxxxxxxxxxx@gmail.com",
        :to => "yyyyyyyyyyyyyygmail.com",
        :subject => 'Thanks for signing up for our amazing app'
  )
 end
end

suggestion_controller.rb

def create
 @suggestion = Suggestion.new(suggestion_params)

 respond_to do |format|
  if @suggestion.save
    SuggestionMailer.new_suggestion().deliver
    format.html { redirect_to suggestions_url, notice: 'Suggestion was 
    successfully created.' }
    format.json { render :show, status: :created, location: 
  @suggestion }
  else
    format.html { render :new }
    format.json { render json: @suggestion.errors, status: 
   :unprocessable_entity }
  end
end

development.rb

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

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

1 个答案:

答案 0 :(得分:0)

配置似乎是正确的。你可以尝试删除域名吗?如果您想使用域名,请使用'gmail.com'代替mail.google.com。以下是我的配置。请注意,我使用的是config.action_mailer.smtp_settings = {而不是ActionMailer::Base.smtp_settings = {。我不确定这是不是一个问题。

  config.action_mailer.raise_delivery_errors = true  

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

  config.action_mailer.perform_deliveries = true

  config.action_mailer.default_options = {from: 'username@gmail.com'}

  # Mail settings
  config.action_mailer.delivery_method = :smtp

  # SMTP settings for gmail
  config.action_mailer.smtp_settings = {
   :address              => "smtp.gmail.com",
   :port                 => 587,
   :user_name            => 'username@gmail.com',
   :password             => 'password',
   :authentication       => "plain",
   :enable_starttls_auto => true
  }

  config.action_mailer.asset_host = 'http://localhost:3000'

修改

正如贾斯汀所说,你必须在谷歌中启用不那么安全的应用程序。同样可以来自底部的here。类似于下图中的内容

enter image description here

从生产环境发送邮件时我遇到了另一个问题。 This帮助我弄清楚了我需要照顾的另一件事。显然,您需要提供对应用的访问权限。你可以从here开始。只需单击继续并等待10分钟即可进行更改。

为了从制作中发送电子邮件,您可以继续这样做。

*旁注:我在Heroku中部署应用程序时遇到了上述问题*