Rails 4 Mailer不发送电子邮件

时间:2014-03-15 13:14:29

标签: ruby-on-rails email ruby-on-rails-4

我尝试使用Rails 4发送电子邮件。服务器没有出现错误,但我无法收到邮箱中的邮件......这是我的配置:

environnements / development.rb

config.action_mailer.delivery_method = :sendmail
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_options = {from: 'no-reply@website.com'}

我发送这样的电子邮件:

user_mailer.rb

def deliver_password_reset_instructions(user)
  mail :to => user.email, :subject => 'Instructions pour la reinitialisation de votre mot de passe'
end

user.rb

def deliver_password_reset_instructions!
  reset_perishable_token!
  UserMailer.deliver_password_reset_instructions(self)
end

有人知道为什么不发送电子邮件吗?

1 个答案:

答案 0 :(得分:4)

您的问题是您正在生成电子邮件,但实际上并未发送电子邮件

UserMailer.deliver_password_reset_instructions(self).deliver

实际上会传递您正在创建的电子邮件。

阅读http://guides.rubyonrails.org/action_mailer_basics.html#walkthrough-to-generating-a-mailer(第2.1.4节 - 致电邮件程序),了解有关如何在您的应用中使用邮件程序的更多详细信息。

编辑: deliver已在Rails 4.2中弃用,支持deliver_nowdeliver_later。对于与以前版本的Rails中的deliver相同的行为,您应该使用deliver_now - deliver_later旨在与ActiveJob和后台作业处理器一起使用。