Reputation: 115
I want to set a header value for all the mails. I want to do it in interceptor, so that I don't have to repeat the code for all the mails.
If I do it inside the mailer function, the code will be like following,
headers['X-Mailgun-Campaign-Id'] = "1234"
But how I can achieve the same using interceptor?
Upvotes: 2
Views: 878
Reputation: 444
In case anyone finds this looking for the same answer I was, which was how to override an existing header in an interceptor, here's what I ended up using.
message.header.fields.select{|f| f.name == 'X-Custom-Header' }
.each{|f| message.header.fields.delete(f) }
// these two following lines work equivalently in Rails 4:
message.headers['X-Custom-Header'] = "1234"
message.header 'X-Custom-Header' => "1234"
Headers are stored internally in an array, not a hash, so in order to "override" you must first remove the header completely. Otherwise your "override" will actually just add another header with the same name, and it's hard to know how that will be handled on the other end.
Upvotes: 3
Reputation: 874
I had a similar problem with setting custom headers in an ActionMailer interceptor. The standard syntax as used within your mailer does not work:
def self.delivering_email(message)
message.headers['X-Mailgun-Campaign-Id'] = "1234"
end
... but this hash-style syntax noted in the source code does work:
def self.delivering_email(message)
message.headers 'X-Mailgun-Campaign-Id' => "1234"
end
Upvotes: 8