I'd like to run some code whenever an email is sent on my app.
As ActionMailer doesn't support after_filter, I would like to use an observer.
The Rails docs mention this in passing, however does not elaborate.
Thanks!
I'd like to run some code whenever an email is sent on my app.
As ActionMailer doesn't support after_filter, I would like to use an observer.
The Rails docs mention this in passing, however does not elaborate.
Thanks!
I'm surprised how little there is in Rails' documentation about this.
Basically, ActionMailer in Rails 3 introduces the use of Interceptors (called before the message is sent) and Observers (after the message is sent).
To set up an Observer, add the following to an initializer:
Now, the
delivered_email
method will run every time your app sends an e-mail. However, you will only have access to the actualMail
message.To register an Interceptor instead, do the same as above, replacing
register_observer
withregister_interceptor
, and renaming the method fromself.delivered_email
toself.delivering_email
.This Railscast was the best source I could find for info on this (they only talk about interceptors, but the concept is the same for observers).