# Canceling Email in the ActionMailer Model

**URL:** <https://www.ruby-forum.com/t/canceling-email-in-the-actionmailer-model/140857>\
**Category:** Rails\
**Created:** [June 19, 2008, 9:41am UTC](https://www.ruby-forum.com/t/canceling-email-in-the-actionmailer-model/140857 "2008-06-19T09:41:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mozmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@Mozmonkey](https://www.ruby-forum.com/u/Mozmonkey)\
**Post date:** [June 19, 2008, 9:41am UTC](https://www.ruby-forum.com/t/canceling-email-in-the-actionmailer-model/140857/1 "2008-06-19T09:41:26Z")

</div>

On my site I’m adding a feature to let users to turn off email  
notifications for certain things. I want to add the code which checks  
these preferences in my ActionMailer model and stop the delivery  
process if the user has that notification preference turned off.

Here’s an example of what I’d like to do:

class Postoffice \< ActionMailer::Base

```
    # The user received an event invitation

```

def invite(event, invite, subject, message)  
user = invite.user  
return if !user.can\_email\_for(:invite)

```
@recipients = invite.user.email
@from = "\"#{event.user.name}\" <removed_email_address@domain.invalid>"
@subject = subject

body[:event] = event

```

end  
end

The user function “can\_email\_for” will check the user’s preferences  
for that type of notification and return true or false. Currently  
this code works, but only because the rhtml template can’t compile  
since the variables aren’t set (the return statement causes this).  
That doesn’t seem like a solid way to stop the email delivery. Is  
there a better way to do this?

I don’t want to put this logic in the code which calls  
Postoffice.deliver\_invite(…) because that’ll cause a lot of  
redundant code and it seems to make more sense to have this sort of  
logic in the ActionMailer model.

Thanks,  
Jeremy

---

<div class="post-metadata">

**Author:** ![Mozmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@Mozmonkey](https://www.ruby-forum.com/u/Mozmonkey)\
**Post date:** [June 20, 2008, 1:13am UTC](https://www.ruby-forum.com/t/canceling-email-in-the-actionmailer-model/140857/2 "2008-06-20T01:13:56Z")

</div>

Any ideas?

---

<div class="post-metadata">

**Author:** ![Mozmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@Mozmonkey](https://www.ruby-forum.com/u/Mozmonkey)\
**Post date:** [June 20, 2008, 1:49am UTC](https://www.ruby-forum.com/t/canceling-email-in-the-actionmailer-model/140857/3 "2008-06-20T01:49:36Z")

</div>

Mozmonkey wrote:

> On my site I’m adding a feature to let users to turn off email  
> notifications for certain things. I want to add the code which checks  
> these preferences in my ActionMailer model and stop the delivery  
> process if the user has that notification preference turned off.
> 
> Here’s an example of what I’d like to do:
> 
> class Postoffice \< ActionMailer::Base
> 
> ```
> # The user received an event invitation
> 
> ```
> 
> def invite(event, invite, subject, message)  
> user = invite.user  
> return if !user.can\_email\_for(:invite)
> 
> ```
> @recipients = invite.user.email
> @from = "\"#{event.user.name}\" <removed_email_address@domain.invalid>"
> @subject = subject
> 
> body[:event] = event
> 
> ```
> 
> end  
> end

There’s no specified way to do that in the API docs  
(although i’d not be surprised if a “return false” did the trick)

otherwise,  
as you say,  
it works at the moment because an exception is raised.

So, don’t rely on this bug.

raise(EmailPermissionsError, “user doesn’t want invite mail”) unless  
user.can\_email\_for(:invite)

however,  
I reckon you’re wrong about putting it in the emailer.

This is a user method.

class User  
def invite(other\_user-)  
return unless other\_user.can\_email\_for(:invite)  
InviteMailer.deliver\_invite…  
end  
end
