【问题标题】:rails 3: how to abort delivery method in actionmailer?rails 3:如何中止 actionmailer 中的传递方法?
【发布时间】:2011-09-26 21:45:34
【问题描述】:

在我的邮件控制器中,在某些情况下(缺少数据)我们会中止发送电子邮件。

在这种情况下,如何在不渲染视图的情况下退出控制器方法?

如果@some_email_data.nil 则返回?

由于视图仍在呈现,因此无法解决问题(在我尝试使用 @some_email_data 的每个地方都会抛出错误,除非我添加了很多 nil 检查)

即使我进行 nil 检查,它也会抱怨没有“发件人”(因为我应该在到达设置发件人和主题的行之前进行“返回”。

render ... return 也没有

基本上,在邮件方法中返回不会返回!

【问题讨论】:

  • 可以贴一些相关代码吗?
  • 在 Rails 2 中是否可以从邮件中返回?

标签: ruby-on-rails actionmailer


【解决方案1】:

比接受的答案更简单的解决方案是:

class SomeMailer < ActionMailer::Base
  def some_method
    if @some_email_data.nil?
      self.message.perform_deliveries = false
    else
      mail(...)  
    end  
  end
end

如果您使用的是 Rails 3.2.9(或更高版本甚至更好) - 您最终可以有条件地调用 mail()。这是相关的GitHub thread。现在可以像这样重写代码:

class SomeMailer < ActionMailer::Base
  def some_method
    unless @some_email_data.nil?
      mail(...)  
    end  
  end
end

【讨论】:

  • 你如何测试这个结果?
  • 好像在 Rails 3.2.9 中你可以看到 mail.should be_a_kind_of NullMail 但我仍然不知道如何在 Rails 3.2.8 中测试它
  • 添加了一些小玩意,我发现你可以在 Rails 3.2.8 中做到这一点:mail.perform_deliveries.should_not be
  • 测试没有交付任何东西:expect { described_class.my_email_method.deliver_now }.not_to change { ActionMailer::Base.deliveries.count }
  • 不应该是mail.perform_deliveries吗? self.message.perform_deliveries = false 对我不起作用。但是mail.perform_deliveries 有效! Rails 指南中也提到了这一点–guides.rubyonrails.org/…
【解决方案2】:

我刚刚在这里遇到了同样的事情。

我的解决方案如下:

module BulletproofMailer
  class BlackholeMailMessage < Mail::Message
    def self.deliver
      false
    end
  end

  class AbortDeliveryError < StandardError
  end

  class Base < ActionMailer::Base

    def abort_delivery
      raise AbortDeliveryError
    end

    def process(*args)
      begin
        super *args
      rescue AbortDeliveryError
        self.message = BulletproofMailer::BlackholeMailMessage
      end
    end
  end
end

使用这些包装邮件会如下所示:

class EventMailer < BulletproofMailer::Base
  include Resque::Mailer
  def event_created(event_id)
    begin
      @event = CalendarEvent.find(event_id)
    rescue ActiveRecord::RecordNotFound
      abort_delivery
    end
  end
end

在我的博客里也是posted。

【讨论】:

  • 我仍然相信这可以更容易地完成,但我不知道如何。
  • @Eisenhorn 有 - 看看我的回答。
  • 我的所有电子邮件都有一个 ApplicationMailer 基类(很像 ApplicationController),我把这个功能放在那里。
【解决方案3】:

我发现这种方法似乎是侵入性最小的方法,因为它适用于所有邮件程序方法,而无需您记住捕获错误。在我们的例子中,我们只需要一个设置来完全禁用某些环境的邮件程序。在 Rails 6 中测试,虽然我确信它在 Rails 5 中也可以正常工作,可能更低。

class ApplicationMailer < ActionMailer::Base
  class AbortDeliveryError < StandardError; end

  before_action :ensure_notifications_enabled
  rescue_from AbortDeliveryError, with: -> {}

  def ensure_notifications_enabled
    raise AbortDeliveryError.new unless <your_condition>
  end

  ...

end

空的 lambda 会导致 Rails 6 只返回一个 ActionMailer::Base::NullMail 实例,该实例不会被传递(就像您的邮件方法没有调用 mail 或提前返回一样)。

【讨论】:

    【解决方案4】:

    设置self.message.perform_deliveries = false 对我不起作用。

    我使用了与其他一些答案类似的方法 - 使用错误处理来控制流程并阻止发送邮件。

    下面的示例是中止在非生产环境中发送邮件到非白名单电子邮件,但辅助方法逻辑可以是您的场景所需的任何内容。

    class BaseMailer < ActionMailer::Base
      class AbortedMailer < StandardError; end
    
      def mail(**args)
        whitelist_mail_delivery(args[:to])
        super(args)
      rescue AbortedMailer
        Rails.logger.info "Mail aborted! We do not send emails to external email accounts outside of Production ENV"
      end
    
      private
    
      def whitelist_mail_delivery(to_email)
        return if Rails.env.production?
    
        raise AbortedMailer.new unless internal_email?(to_email)
      end
    
      def internal_email?(to_email)
        to_email.include?('@widgetbusiness.com')
      end
    end
    

    【讨论】:

      【解决方案5】:

      我只是清除了@to 字段并返回,所以当它没有任何内容时传递中止。 (或者在设置@to 之前返回)。

      【讨论】:

      • 它可能会中止,但你知道这是一个垃圾答案。如果有抄送/密送,电子邮件仍然会发出,此外,由于这是一种黑客行为,因此无法保证将来不会引发巨大的错误。
      • 它比(当前)顶级解决方案更优雅。优雅与复杂的权衡。 Rails 显然需要添加一些东西。
      • “优雅”,喜欢它:-P
      【解决方案6】:

      我没有花太多时间使用 rails 3,但您可以尝试使用

      redirect_to some_other_route

      或者,如果您真的只是检查丢失的数据,您可以对表单字段进行 js 验证,只有在通过时才提交。

      【讨论】:

      • 没有用户或会话可以重定向到...(该方法被延迟作业调用以响应某些事件触发器)我们需要该方法简单地停止在其轨道上
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-29
      • 2011-06-24
      • 1970-01-01
      • 2011-07-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多