Skip to content

Instantly share code, notes, and snippets.

@ryw
Created October 8, 2010 19:25
Show Gist options
  • Save ryw/617373 to your computer and use it in GitHub Desktop.
Save ryw/617373 to your computer and use it in GitHub Desktop.
#######################
# Example Usage:
#######################
#
# mailbox = BadMailProcessor::Pop3.new(server, username, password)
# BadMailProcessor.run(mailbox) do |bounce|
# User.bounce(bounce[:email_address])
# my_action2
# end
#
class BadMailProcessor
EMAIL_REGEX = /\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i
def self.run(mailbox)
mailbox.bounces.each do |bounce|
yield { :email_address => bounce[:email_address], :bounce_code => bounce[:bounce_code] }
end
end
end
class BadMailProcessor::Pop3
attr_accessor :bounces
def initialize
conn = Net::POP3.new(server)
conn.start(username, password)
conn.emails.each do |email|
tmail = TMail::Mail.parse(email.pop)
bounce = BounceEmail::Mail.new(tmail)
if bounce.isbounce && bounce.type =~ /Permanent/
email_address = tmail.body.scan(BadMailProcessor::EMAIL_REGEX).compact.
reject { |address| address =~ /recruitmilitary\.com$/i }.first
@bounces << { :email_address => :email_address, :bounce_code => bounce.code } if email_address
end
email.delete
end
conn.finish
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment