简体   繁体   English

Rails Action Mailer 不发送电子邮件

[英]Rails Action Mailer not sending email

I am a complete beginner in Rails and I'm trying to send an email after someone signs up using Action Mailer.我是一个完整的 Rails 初学者,我正在尝试在有人使用 Action Mailer 注册后发送电子邮件。

My logs say that the email is sending, but Gmail never gets it.我的日志说电子邮件正在发送,但 Gmail 从未收到。

config/initializers/setup_mail.rb配置/初始化程序/setup_mail.rb

ActionMailer::Base.smtp_settings = {
  :address              => "smtp.gmail.com",
  :port                 => 587,
  :domain               => "asciicasts.com",
  :user_name            => "asciicasts",
  :password             => "secret",
  :authentication       => "plain",
  :enable_starttls_auto => true
}

mailers/user_mailer.rb邮件程序/user_mailer.rb

class UserMailer < ActionMailer::Base
  default :from => "eifion@asciicasts.com"

  def registration_confirmation(user)
    mail(:to => user.email, :subject => "Registered")
  end
end

controllers/users_controller.rb控制器/users_controller.rb

...
def create
    @user = User.new(params[:user])
    if @user.save
      UserMailer.registration_confirmation(@user).deliver
      sign_in @user
      flash[:success] = "Welcome to the Sample App!"
      redirect_to @user
    else
      render 'new'
    end
  end
...

Thanks!谢谢!

Make sure you have this option set in your config/environments/development.rb :确保您在config/environments/development.rb设置了此选项:

config.action_mailer.delivery_method = :smtp

Also, in ActionMailer::Base.smtp_settings you need to specify a valid gmail account.此外,在ActionMailer::Base.smtp_settings您需要指定一个有效的 gmail 帐户。 Copy-pasting (asciicasts) is not gonna cut it here.复制粘贴(asciicasts)不会在这里剪切。

See this question for reference: Sending mail with Rails 3 in development environment请参阅此问题以供参考: 在开发环境中使用 Rails 3 发送邮件

Instead of 'smtp' you can use 'sendmail'您可以使用“sendmail”代替“smtp”

ActionMailer::Base.delivery_method = :sendmail

ActionMailer::Base.sendmail_settings = { :address => "smtp.gmail.com",
     :port => "587", :domain => "gmail.com", :user_name => "xxx@gmail.com", 
    :password => "yyy", :authentication => "plain", :enable_starttls_auto => true }

I ran into this same problem for a new mailer I had setup.对于我设置的新邮件程序,我遇到了同样的问题。 I couldn't figure out for the life of me why this new mailer couldn't send emails, or even get to the method in the mailer when I stepped through it.我终生无法弄清楚为什么这个新邮件程序无法发送电子邮件,甚至在我浏览邮件程序时无法找到邮件程序中的方法。

Solution解决方案

It ended up being that if you put the deliver_now or deliver* code within the mailer, it does not send the email.最终结果是,如果您将deliver_nowdeliver_now deliver*代码放在邮件程序中,它就不会发送电子邮件。

Example Broken示例损坏

  def email_message()
    message = mail(to: User.first, subject: 'test', body: "body text for mail")
    message.deliver_now
  end

Corrected更正

  #Different class; in my case a service
  def caller
    message = MyMailer.email_message
    message.deliver_now
  end

  def email_message()
    mail(to: User.first, subject: 'test', body: "body text for mail")
  end

This solved the problem for me, I hope it solves it for someone else.这为我解决了问题,我希望它为其他人解决了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM