Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails mail interceptor only for a specific mailer

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?

like image 269
Augustin Riedinger Avatar asked Oct 16 '13 16:10

Augustin Riedinger


People also ask

What is the use of email interceptor class?

These allow you to register classes that are called during the mail delivery life cycle of every email sent. Interceptors allow you to make modifications to emails before they are handed off to the delivery agents. An interceptor class must implement the ::delivering_email (message) method which will be called before the email is sent.

What is an interceptor in Salesforce?

An interceptor is a special class that has a delivering_email (mail) method. The delivering_email method is what will be called before the email is actually sent. Inside the method we'll be able to interact and modify our email before it is sent by the original mailer. So simple but so awesome.

How to send emails from other controller actions in rails?

Once the Rails mailer and Gmail settings are properly configured, we can easily send other emails from other controller actions by generating and setting up new mailers in much the same way. :) I just wanted to thank you for writing out this detailed tutorial.

What is Action Mailer in rails?

What is action mailer? According to the Ruby on Rails Guides, “Action Mailer allows you to send emails from your application using mailer classes and views”. Before going any further, if you aren’t familiar with the Model-View-Controller pattern, it might help to take some time to read up on it as Action Mailer relies on the same design patterns.


2 Answers

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 = '[email protected]'
    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.

like image 142
Augustin Riedinger Avatar answered Nov 10 '22 00:11

Augustin Riedinger


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.

like image 25
Intrepidd Avatar answered Nov 10 '22 00:11

Intrepidd