14

Nov

Rails 3 :: Not abandon sending mail within ActionMailer action

In ActionMailer (Rails 3) you can’t decide not to send an email. For instance, you have the following mailer:

class FooMailer < ActionMailer::Base
  def bar_email
    if some_condition
      mail(...)
    else
      # Can I do nothing? No :-(
    end
  end
end

If you invoke FooMailer.bar_email.deliver!, when same_condition is false you will get the following error:

ArgumentError: A sender (Return-Path, Sender or From) required to send a message

The workaround for that is move the condition to the place where you are making the call to FooMailer.bar_email.deliver!