Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

v

  • Loading branch information...
commit 0b88829e37038408fa683b9e338eae42cffbbca3 1 parent 6c7664a
@dmarkow dmarkow authored
Showing with 60 additions and 5 deletions.
  1. +60 −5 lib/tmail_bounce_parser.rb
View
65 lib/tmail_bounce_parser.rb
@@ -6,16 +6,71 @@
module TmailBounceParser
class BouncedDelivery
- attr_accessor :status_info, :original_message_id, :original_sender, :original_recipient, :original_subject, :handling_server
+ attr_accessor :status_info, :original_message_id, :original_sender, :original_recipient, :original_subject, :handling_server, :status_part
def self.from_email(email)
+ bounce = self.new
+ # lets start by trying to extract the message/delivery-status part
+
+ bounce.status_part = email.parts.detect { |part| part.content_type == "message/delivery-status" }
+ if !bounce.status_part.nil?
+ statuses = bounce.status_part.body.gsub("\n ","").split(/\n/)
+ bounce.status_info = statuses.inject({}) do |hash,line|
+ key,value = line.split(/:/,2)
+ hash[key] = value.strip rescue nil
+ hash
+ end
+ original_message_part = email.parts.detect do |part|
+ part.content_type == "message/rfc822"
+ end
+ unless original_message_part.nil?
+ parsed_msg = TMail::Mail.parse(original_message_part.body)
+ bounce.original_message_id = parsed_msg.message_id
+ else
+ original_message_part = email.parts.detect do |part|
+ part.content_type == "text/rfc822-headers"
+ end
+ unless original_message_part.nil?
+ parsed_msg = TMail::Mail.parse(original_message_part.body)
+ bounce.original_message_id = parsed_msg.message_id
+ end
+ end
+
+ else
+ if (email['subject'].to_s == "Mail delivery failed: returning message to sender")
+ bounce.handling_server = :exim
+
+ original_message_part = email.body
+ bounce.original_message_id = original_message_part[/Message.*$/].split(" ")[1] rescue "N/A"
+ arr = original_message_part.split("\n")
+ for i in 0..arr.length-1
+ if arr[i][/The following address/]
+ j = i + 3
+ status = []
+ while arr[j].strip != ""
+ status << arr[j].strip
+ j += 1
+ end
+ bounce.status_info = {}
+ bounce.status_info['Status'] = status.join(" ")
+ break
+ end
+ end
+ end
+
+ end
+
+ bounce
+ end
+ def test
returning(bounce = self.new) do
-
+
+ # We need to figure out which server sent the rejection notice first
if (email['subject'].to_s =~ /not listed in Domino Directory/)
- bounce.handling_server = "DOMINO"
+ bounce.handling_server = :domino
elsif (email['subject'].to_s =~ /Mail delivery failed: returning message to sender/)
- bounce.handling_server = "EXIM"
+ bounce.handling_server = :exim
else
- bounce.handling_server = "STANDARD"
+ bounce.handling_server = :standard
end
# Domino mail servers munge the "original message id" to something completely different
Please sign in to comment.
Something went wrong with that request. Please try again.