所有邮件似乎都被忽略了(邮件不是在开发或暂存中生成的)

All mailers seemingly being ignored (mail not being generated in development or staging)

我一直在努力将我们的应用程序从 Rails 3.2 升级到 4.2。当我在浏览器中进行一些测试时,我注意到在开发过程中我的浏览器中没有通过 letter_opener gem 生成任何电子邮件,并且在暂存阶段也没有发送任何电子邮件。我在进行测试时开始查看控制台日志,发现甚至没有生成邮件模板。没有错误。当我切换回我的主分支(使用 Rails 3)时,一切正常。就像我的邮件程序被忽略了一样。

例如,用户模型有一个 send_welcome_email 回调,如下所示:

# user.rb

after_create :send_welcome_email

def send_welcome_email
  if self.email
    CustomerMailer.welcome(self.id)
  end
end

welcome 方法如下所示:

# customer_mailer.rb

  def welcome(user_id)
    @user = User.find(user_id)
    msg = mail(subject: "Welcome to Sweeps!",
              to: @user.email,
              tag: 'customer welcome')
    msg.deliver  # also tried .deliver_now
  end

如果我将 binding.pry 放在 CustomerMailer.welcome 方法中,我永远不会像我期望的那样在我的日志中收到控制台提示,这就是为什么我认为邮件程序被完全忽略的原因。

这是我的环境的邮件设置:

# development.rb

  config.action_mailer.raise_delivery_errors = false
  config.action_mailer.default_url_options = { host: 'localhost:3000' }
  config.action_mailer.asset_host = "http://localhost:3000"
  config.action_mailer.perform_deliveries = true
  config.action_mailer.delivery_method = :letter_opener

# staging.rb

  config.action_mailer.delivery_method = :mandrill
  config.action_mailer.default_url_options = { :host => 'staging.myapp.com' }
  config.action_mailer.asset_host = "http://staging.myapp.com"
  config.action_mailer.raise_delivery_errors = true

根据 the doc,您可能只是没有要求邮寄人使用 deliver_nowdeliver_later 实际投递邮件。这应该在邮件程序 class 的 外部 调用,以便调用邮件程序方法。

# user.rb

after_create :send_welcome_email

def send_welcome_email
  if self.email
    CustomerMailer.welcome(self.id).deliver_now
  end
end