ActionMailer cancel send from one of deliver_* methods

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?

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.