# Before\_filter for ActionMailer

**URL:** <https://www.ruby-forum.com/t/before-filter-for-actionmailer/129159>\
**Category:** Rails\
**Created:** [February 15, 2008, 12:03am UTC](https://www.ruby-forum.com/t/before-filter-for-actionmailer/129159 "2008-02-15T00:03:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85f322/32.png) [@jonzo](https://www.ruby-forum.com/u/jonzo)\
**Post date:** [February 15, 2008, 12:03am UTC](https://www.ruby-forum.com/t/before-filter-for-actionmailer/129159/1 "2008-02-15T00:03:41Z")

</div>

I’d like to execute some kind of before filter in my ActionMailer  
class… does anyone know how to do this?

Specifically I let my users turn off email notifications, and I’d like  
to check their notification settings before any email gets sent. Seems  
like the wrong thing to do to have a million if statements lying all  
over the place whenever emails are delivered…

Thanks

---

<div class="post-metadata">

**Author:** ![jonzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85f322/32.png) [@jonzo](https://www.ruby-forum.com/u/jonzo)\
**Post date:** [February 15, 2008, 12:13am UTC](https://www.ruby-forum.com/t/before-filter-for-actionmailer/129159/2 "2008-02-15T00:13:40Z")

</div>

On Fri, Feb 15, 2008 at 10:02 AM, Jonzo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’d like to execute some kind of before filter in my ActionMailer  
> class… does anyone know how to do this?

> Specifically I let my users turn off email notifications, and I’d like  
> to check their notification settings before any email gets sent. Seems  
> like the wrong thing to do to have a million if statements lying all  
> over the place whenever emails are delivered…

You would be better off asking your users model for a list of people  
to send to who are subscribed.

Your mailer part of the app has to find the user to get the email  
address, just set up a custom finder in the user model that refuses to  
give out the user object to the mailer if the user has unsubscribed on  
their record.

Regards

Mikel

---

<div class="post-metadata">

**Author:** ![jonzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85f322/32.png) [@jonzo](https://www.ruby-forum.com/u/jonzo)\
**Post date:** [February 15, 2008, 12:30am UTC](https://www.ruby-forum.com/t/before-filter-for-actionmailer/129159/3 "2008-02-15T00:30:10Z")

</div>

Are you thinking about newsletter situations?

Ususally when I send an email I already have the user object in my  
hand (most of my emails are for things like watchlist changes,  
questions from other users, etc.), so an if statement to check whether  
the user wants to receive emails would be a lot cheaper than doing  
another database query.

What about inside the mailer methods? can I return false or throw an  
exception and be sure that the email won’t be delivered? Is there any  
way to control whether the email is delivered from inside the mailer  
object instead of relying on the caller?

---

<div class="post-metadata">

**Author:** ![jonzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85f322/32.png) [@jonzo](https://www.ruby-forum.com/u/jonzo)\
**Post date:** [February 15, 2008, 10:35am UTC](https://www.ruby-forum.com/t/before-filter-for-actionmailer/129159/4 "2008-02-15T10:35:59Z")

</div>

Yeah thanks Mikel, that’s what I ended up doing in the end. I created  
new methods in my mailer class that check to see if the user wants to  
be sent emails, if so then it’s sent, otherwise it’s stored in a  
messages table that the user can check next time they sign into the  
website.

Thanks for the help and suggestions!

On Fri, Feb 15, 2008 at 10:06 PM, Mikel L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > the user wants to receive emails would be a lot cheaper than doing  
> > and have a boolean value in your table called “subscribed”  
> > [http://lindsaar.net/](http://lindsaar.net/)
> 
> >

–  
Jonathan

---

<div class="post-metadata">

**Author:** ![jonzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85f322/32.png) [@jonzo](https://www.ruby-forum.com/u/jonzo)\
**Post date:** [February 15, 2008, 10:07am UTC](https://www.ruby-forum.com/t/before-filter-for-actionmailer/129159/5 "2008-02-15T10:07:06Z")

</div>

On Fri, Feb 15, 2008 at 10:29 AM, Jonzo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Are you thinking about newsletter situations?

Yes

> Ususally when I send an email I already have the user object in my  
> hand (most of my emails are for things like watchlist changes,  
> questions from other users, etc.), so an if statement to check whether  
> the user wants to receive emails would be a lot cheaper than doing  
> another database query.

Well, if you have done a @user = User.find(123) then doing  
@user.wants\_email? shouldn’t generate another DB query as it should  
hit the ActiveRecord cache.

Why can’t you just do:

deliver\_email if @user.subscribed

and have a boolean value in your table called “subscribed”

I think that would be about as clean and dry as can be.

If you want to put it in your create\_email method, you will have to  
make a method in your Notifier that creates the email but checks the  
users subscription status first…

Regards

Mikel

> **[Mikel Lindsaar](https://lindsaar.net/)**
>
> Internet Home for Mikel Lindsaar
