Rails mail interceptor only for a specific mailer

末鹿安然 提交于 2019-12-19 08:29:10

问题


I have an interceptor : DevelopmentMailInterceptor and an inititializer setup_mail.rb that initiates the interceptor.

But I want to apply it to a specific mailer (to intercept NotificationMailer and not the other ones.

So I set in setup_mail.rb :

`NotificationMailer.register_interceptor(DevelopmentMailInterceptor) 

But then all mailers get intercepted, as if I'd written

ActionMailer::Base.register_interceptor(DevelopmentMailInterceptor)

How can I filter this?


回答1:


I found the solution by exploring in depth the message parameter of the delivering_email method of the mailer interceptor.

class DevelopmentMailInterceptor
  def self.delivering_email(message)

    filteredMailers = %w[
      NotificationMailer
    ]

    if filteredMailers.include?(message.delivery_handler)
      message.subject = "[filter] To:#{message.to} - #{message.subject}"
      message.to = 'logs@mail.com'
    end

    return message
  end
end

@Intrepidd's answer remains true since the interceptor is applied to the whole Mail class but I found the way around to what I wanted to achieve.




回答2:


You can't.

Interceptor are registered on the global Mail class (see the source of register_interceptor)

If you need your interceptor to be effective only on specific e-mails, you should add a condition inside the interceptor class.



来源:https://stackoverflow.com/questions/19408376/rails-mail-interceptor-only-for-a-specific-mailer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!