简体   繁体   English

仅针对特定邮件程序的Rails邮件拦截器

[英]Rails mail interceptor only for a specific mailer

I have an interceptor : DevelopmentMailInterceptor and an inititializer setup_mail.rb that initiates the interceptor. 我有一个拦截器: DevelopmentMailInterceptor和一个启动拦截器的inititializer setup_mail.rb

But I want to apply it to a specific mailer (to intercept NotificationMailer and not the other ones. 但我想将它应用于特定的邮件程序(拦截NotificationMailer而不是其他邮件。

So I set in setup_mail.rb : 所以我在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? 我该如何过滤?

I found the solution by exploring in depth the message parameter of the delivering_email method of the mailer interceptor. 我通过在深度探索找到的解message的参数delivering_email邮件收发器拦截器的方法。

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. @ Intrepidd的答案仍然是正确的,因为拦截器应用于整个Mail类,但我找到了实现我想要的方法。

You can't. 你不能。

Interceptor are registered on the global Mail class (see the source of register_interceptor) 拦截器在全局Mail类上注册(参见register_interceptor的源代码)

If you need your interceptor to be effective only on specific e-mails, you should add a condition inside the interceptor class. 如果您需要拦截器仅对特定电子邮件有效,则应在拦截器类中添加一个条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM