Rails 邮件程序未抛出任何错误,但电子邮件未到达

Rails mailer throws no error, but email doesn't arrive

我的 Rails 邮件程序在几个月前运行良好。好久没处理了,也不记得改过什么了。但是现在,当我通过我的控制台激活邮件程序时,没有发送电子邮件,尽管我没有收到任何错误。

我仔细检查了电子邮件地址和密码是否正确。我还根据 this answer 设置了我的电子邮件。但是没有发送电子邮件。如果出现问题,我不应该至少在我的控制台中收到错误消息吗?

我正在使用 Rails 4.0.10.

config/environments/development.rb

Website::Application.configure do
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.default_url_options = { host:'localhost', port: '3000' }
  config.action_mailer.perform_deliveries = true
  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.default :charset => "utf-8"
  config.action_mailer.smtp_settings = {
    address: 'smtp.gmail.com',
    port: 587,
    domain: 'localhost:3000',
    user_name: 'xxxxxx@xxxxxxx',
    password: 'password',
    authentication: 'plain',
    enable_starttls_auto: true
  }
end

app/mailers/user_mailer

class UserMailer < ActionMailer::Base
  default from: "xxxxxx@xxxxx"

  def notify(user)
    @user = user
    mail(to: @user.email,subject: "subject")
  end
end

控制台

$ UserMailer.notify(User.first)
 Rendered user_mailer/notify.html.erb (0.4ms)
=> #<Mail::Message:2623523, Multipart: false, Headers: <From: xxxxxx@xxxxx>, <To: xxxxxxx@xxxxx>, <Subject: subject>, <Mime-Version: 1.0>, <Content-Type: text/html>> 

您必须对 notify 方法的结果调用 .deliver_now.deliver_later。查看如何使用 ActionMailer 了解更多信息。

controllerModel

中使用 Mailer 方法 deliverdeliver_nowdeliver_later
UserMailer.notify(User.first).deliver