【问题标题】:ActionMailer::Base::NullMail when trying exception_notification in developmentActionMailer::Base::NullMail 在开发中尝试 exception_notification 时
【发布时间】:2013-02-25 16:26:01
【问题描述】:

我想将 exception_notification gem 添加到我们的应用程序中,但是,当我尝试手动触发邮件时会发生这种情况:

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>

在上面的示例中,控制台位于 ApplicationController 中的 rescue_from Exception 内的断点处,之后是某个控制器中故意的 1/0

我也在使用delayed_job,但是 - 不足为奇 - ExceptionNotifier::Notifier.background_exception_notification(exception).deliver 没有假脱机。

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

有什么想法我在这里做错了吗?感谢您的帮助!

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 exception-notification exception-notifier


    【解决方案1】:

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

    来自documentation

    class Notifier < ActionMailer::Base
      default :from => 'no-reply@example.com',
             :return_path => 'system@example.com'
    
      def welcome(recipient)
        @account = recipient
        mail(:to => recipient.email_address_with_name,
             :bcc => ["bcc@example.com", "Order Watcher <watcher@example.com>"])
      end
    end
    

    【讨论】:

      【解决方案2】:

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

      def my_mail(foo)
         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()
      end
      

      ruby 文档中没有立即明确的是您需要返回邮件功能,而不仅仅是执行它。如果您在构建邮件对象后需要做某事,请确保在最后返回邮件。像这样:

      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
      

      【讨论】:

        猜你喜欢
        • 2021-11-19
        • 1970-01-01
        • 1970-01-01
        • 2014-12-12
        • 2011-12-05
        • 2013-02-21
        • 2011-12-19
        • 2014-01-13
        • 1970-01-01
        相关资源
        最近更新 更多