Passing custom attributes to mail observer

I was trying to figure out how to pass custom attributes to a Ruby on Rails mail observer (should be the same for an interceptor) and couldn’t find any documentation about it. Since the observer only receives the Mail object as a parameter in


self.delivered_email

method, we will need to pass the custom attributes through the mail object. One way to do is as follows:


mail(
  :to =>'email@sample.com',
  :custom_attr_1 => 'This is my custom string'
)

And you can receive it in the observer with the following:


def self.delivered_email(mail)
  puts mail.header[:custom_attr_1].value
end

If you have an interceptor, use the following:


def self.delivering_email(mail)
  puts mail.header[:custom_attr_1].value
end

I hope this helps someone.

Leave a Reply