Skip to content
Provides Callbacks for ActionMailer
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


= ActionMailer Callbacks

ActionMailer callbacks provides basic callback support for ActionMailer.  It providers similar functionality to before and after filters available in ActionController.

== Installation

Install as a plugin in your vendor/plugins directory.

If your rails version supports it, you can do:

script/plugin install git://

== How Do I Use It?

There are 2 main ways to define a callback. In each case, the callback method/block is passed the mail object as the only argument.

You may define a callback using a block:
class FooMailer < ActionMailer::Base
  after_deliver do |mail|

You may also define a callback using a symbol/string for a method name:
class FooMailer < ActionMailer::Base
  before_deliver :append_advertisement

  def append_advertisement(mail)

Callbacks take options which can be used to define which mail types (i.e. methods) they will be applied to.
These options take the format of *only* and *except*.
  - An *only* callback will only be run for methods which match the passed method names.
  - An *except* callback will be called for all methods EXCEPT those that match the passed method names
The options can take either an array of strings/symbols, or a single string/symbol.

class FooMailer < ActionMailer::Base
  before_deliver :append_disclaimer, :only => [:email_friend, :announce_something]
  after_deliver :notify_user, :except => :invite_user


== What About Halting the Chain?

You can halt the chain in either a before or after callback.  In order to do this, just call +halt_callback_chain+ in the block (or +self.class.halt_callback_chain+ in an instance method).
If the chain is halted in a before callback, the email will *NOT* be delivered and no other callbacks will be invoked (either any after callbacks or any remaining before callbacks).
If the chain is halted in an after callback, the email will have already been sent and all before callbacks would have run, but any remaining after callbacks will not be invoked.

class FooMailer < ActionMailer::Base
  before_deliver do |mail|
    halt_callback_chain if invalid_mail?(mail)

  after_deliver :abort

  def abort(mail)

== Anything Planned?

First, I would like to clean up the code a bit (extract most of the logic into a separate module, etc.).  I apologize for the messiness of the code, at the time I wrote it I just wanted something "that worked" and I was pressed for time.

In terms of features, I would like to do at least three more things with this:
1) Add an around_deliver callback which would allow around advice
2) Add ability to skip filters
3) Add ability to prepend filters in the chain

Feedback is welcomed.

Copyright (c) 2009 Anthony Caliendo, released under the MIT license
Something went wrong with that request. Please try again.