4

试图exception_notification我想给exception_notification宝石添加到我们的应用程序,但是,这种情况发生时,我尝试手动触发邮件:的ActionMailer :: Base的:: NullMail当发展

exception 
# => #<ZeroDivisionError: divided by 0> 
ExceptionNotifier::Notifier.exception_notification(request.env, exception) 
# => #<ActionMailer::Base::NullMail:0x007fa81bc7c610> 
ExceptionNotifier::Notifier.background_exception_notification(exception) 
# => #<ActionMailer::Base::NullMail:0x007fa81bf58190> 

在上面的例子中,控制台是在某些控制器故意1/0后内部在ApplicationController中rescue_from Exception一个断点。

我使用delayed_job的为好,但 - 毫无疑问 - ExceptionNotifier::Notifier.background_exception_notification(exception).deliver没有任何后台。

我已经设置在发展config.consider_all_requests_local = false,但仍exception_notification实例NullMail。在应用程序的其他部分,邮件工作正常,并使用sendmail

任何想法我在做什么错在这里?谢谢你的帮助!

回答

7

可能是你使用的是旧版本的ExceptionNotifier和ActiveMailer :: Base的较新版本。不在电子邮件功能中调用邮件命令将导致返回ActionMailer :: Base :: NullMail实例,而不是Mail实例。

documentation

class Notifier < ActionMailer::Base 
    default :from => '[email protected]', 
     :return_path => '[email protected]' 

    def welcome(recipient) 
    @account = recipient 
    mail(:to => recipient.email_address_with_name, 
     :bcc => ["[email protected]", "Order Watcher <[email protected]>"]) 
    end 
end 
2

我有我的测试/ rspec的返回NullMail对象。该解决方案很简单,我的代码是:

什么不是从红宝石文档立即清晰的是,你需要返回的邮件功能,而不仅仅是执行它。如果您在构建邮件对象后需要做某些事情,请确保在最后返回邮件。像这样:

def my_mail(foo) 
    m = mail(
    to: to, 
    from: from, 
    subject: @sample_item.campaign_market.campaign.translation_for(language_id, 'sample_item_mailer.request_review.subject'), 
    content_type: "text/html" 
    ) 
    @sample_item.update_attributes!({feedback_requested: true, review_requested_at: Time.now}) 
    TrackingService::Event.new(@user, @user.market, 'sample_items', "request_review_email #{@sample_item.id}").call() 
    return m 
end