# ActionMailer cancel send from one of deliver\_\* methods

**URL:** <https://www.ruby-forum.com/t/actionmailer-cancel-send-from-one-of-deliver-methods/107202>\
**Category:** Rails\
**Created:** [June 19, 2007, 4:17am UTC](https://www.ruby-forum.com/t/actionmailer-cancel-send-from-one-of-deliver-methods/107202 "2007-06-19T04:17:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![surge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@surge](https://www.ruby-forum.com/u/surge)\
**Post date:** [June 19, 2007, 4:17am UTC](https://www.ruby-forum.com/t/actionmailer-cancel-send-from-one-of-deliver-methods/107202/1 "2007-06-19T04:17:23Z")

</div>

Seems like executing a return in one of the deliver\_\* methods of  
ActionMailer doesn’t cancel the delivery. The template is still read  
right after the return method has been executed and because all  
variables in the template are empty, all kinds of errors occur.

I need to validate the email (and test whether it actually exists)  
before a delivery is made and I’m trying to put all that logic inside  
the delivery methods themselves, but no luck.

Anybody know of a solution?

---

<div class="post-metadata">

**Author:** ![surge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@surge](https://www.ruby-forum.com/u/surge)\
**Post date:** [November 5, 2008, 8:23pm UTC](https://www.ruby-forum.com/t/actionmailer-cancel-send-from-one-of-deliver-methods/107202/2 "2008-11-05T20:23:07Z")

</div>

surge wrote:

> Seems like executing a return in one of the deliver\_\* methods of  
> ActionMailer doesn’t cancel the delivery. The template is still read  
> right after the return method has been executed and because all  
> variables in the template are empty, all kinds of errors occur.
> 
> I need to validate the email (and test whether it actually exists)  
> before a delivery is made and I’m trying to put all that logic inside  
> the delivery methods themselves, but no luck.
> 
> Anybody know of a solution?

I needed this myself just now, and found this thread (on Ruby Forum), so  
I figured I’d post my solution here for others to find, even though the  
question was well over a year ago:

In my ApplicationMailer that I inherit all others mailers from, I do  
this:

# Allow cancelling mails from within a mailer by doing "raise

NotSendingMail".  
class NotSendingMail \< StandardError; end  
def self.method\_missing(\*args)  
super  
rescue NotSendingMail =\> e  
RAILS\_DEFAULT\_LOGGER.info(“Not mailing! #{e}”)  
end

Should be pretty self-explanatory.

I suppose one could use throw/catch instead of raise/rescue, but I find  
it reads worse if you want the equivalent of a rescue block.
