Avoid Emailing Bounced Addresses

In the last post, I explained how I use VERP to handle email bounces and mark bad email addresses. I use email addresses as login handles, so I can’t just delete accounts with email addresses that have become invalid. However, I still want to avoid sending emails to addresses with permanent failures. The address verification needs to happen in one central location before emails are sent. There are numerous actions on the site that trigger notification emails, and I don’t want to every programmer to have to remember to check the status of an email address before sending an email.

I aliased the perform_delivery_sendmail() method in ActionMailer to my own gatekeeper method that checks the status of the recipient address before sending an email. Email is my own ActiveRecord model that stores the status of each address. Here’s the code, which I placed in a file called email_gatekeeper.rb in the lib directory of my Rails project:

module ActionMailer
  class Base

    private

    def perform_delivery_sendmail_with_gatekeeper(mail)
      ignore = false

      if (mail.to.size() == 1)
        email_address = Email.find_by_address(mail.to[0])
        if (email_address && (email_address.status == Email::BOUNCED))
          ignore = true
        end
      end

      perform_delivery_sendmail_without_gatekeeper(mail) unless (ignore)
    end

    alias_method_chain :perform_delivery_sendmail, :gatekeeper

  end
end

Posted

in

by

Tags:

Comments

4 responses to “Avoid Emailing Bounced Addresses”

  1. Bala Avatar
    Bala

    Hi,

    I have created alias for perform_delivery_smtp and i have created gatekeeper method.

    but its not working. how do we make it work. i feel that control itself not coming to gatekeeper method.because i am debugging gatekeeper method. not able to find any debugging statement.

    Thanks & Regards,
    Bala.

  2. Ryan Avatar

    Hey Bala, I had the same problem too, and after looking at the rails documentation, I discovered that :perform_delivery_sendmail doesn’t exist anymore.

    I’m using rails 2.3, and I had to change it to:
    alias_method_chain :delivery!, :gatekeeper

    Then rename the method above as:
    def deliver_with_gatekeeper!(mail = @mail)

    Then at the end of the method, you’ll need to change it to this:
    deliver_without_gatekeeper!(mail)

    Basically, you’re just changing it to use deliver! instead of perform_delivery_sendmail.

    If you’re using Rails 3.0, it’s possible that they’ve changed it to something else, so, just look in the ActionMailer documentation in the Rails docs for whatever looks to be the last stop before sending the email.

  3. Ryan Avatar

    Actually, no…I am a crazy person. The code in this post wasn’t working for me until I discovered that:

    a) I needed to use perform_delivery_smtp and
    b) if you create this file in your lib directory, you’ll probably need to add “require_dependency email_gatekeeper.rb” to the bottom of your environment.rb file.

  4. Ryan Avatar

    Ugh…that should be “require_dependency email_gatekeeper” without the .rb.

    I’m up late…