Handling Unsubscribe Requests in Ruby On Rails With ActionMailer

If you're sending non-transactional email, you should be handling unsubscribe requests. So let's assume you have a database table of unsubscribed emails, and want to check this table before sending anyone an email. In RoR, the quick and dirty solution is to check for unsubscribe status before calling any deliver_action method in your mailer classes:


MyMailer.deliver_action(address) unless unsubscribed(address)

A better solution is to modify the ActionMailer::Base class with with a new recipients method, so that your mailer classes can just call your modified method. Here's what I did:

So now in my mailer class, I just call:


valid_recipients addr

instead of


recipients addr

and anytime I attempt a delivery, unsubscribed addresses will be automatically removed from the recipient list.