Rails mail interceptor only for a specific mailer

浪尽此生 提交于 2019-12-01 06:20:45
Augustin Riedinger

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.

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.

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