mwrsvw
Last Updated: February 25, 2016
·
3.444K
· asanghi

Rails 4: before_filter and after_filter in your Mailers

Did you know that you can use filters in your mailers in Rails 4?

I thought it was a such a cool hidden feature.

Here is what you could possibly do in your mailer's after_filter for example.

class UserMailer < ActionMailer

  after_filter :stop_delivery

  def thismail(user,..)
    @user = user
     ...
  end

  def thatmail(user,..)
    @user = user
     ...
  end

  private

    def stop_delivery
      mail.perform_deliveries = @user.confirmed?
      true
    end

end