Canceling Email in the ActionMailer Model

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}\" <site@mixermixer3.com>"     @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

Any ideas?

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}\" <site@mixermixer3.com>"     @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