Last active
August 29, 2015 14:04
-
-
Save temochka/22399567b4012eb7760d to your computer and use it in GitHub Desktop.
Parsing a raw Postmark bounce dump. Ruby 2.0.0, mail 2.5.4, postmark 1.1.0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'mail' | |
require 'postmark' | |
client = Postmark::ApiClient.new('xxxx-xxxx-xxxx-xxxx') | |
bounce = client.get_bounces(offset: 0, count: 1).first | |
dump = client.dump_bounce(bounce[:id]) | |
msg = Mail::Message.new(dump[:body]) | |
headers = Hash[*msg.header.fields.flat_map { |f| [f.name, f.value] }] | |
# => {"Return-Path"=>"", "Date"=>"Tue, 29 Jul 2014 05:50:48 -0400", "From"=>"", "To"=>"", "Message-ID"=>"", "Subject"=>"Delivery report", "Mime-Version"=>"1.0", "Content-Type"=>"multipart/report; report-type=delivery-status; boundary=\"\"", "Content-Transfer-Encoding"=>"7bit", "X-Your-Custom-Header"=>"Value"} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bounce = Mail::Message.new(dump[:body]) | |
# Find a part containing original message headers | |
original_message_headers = bounce.parts.find { |p| p['Content-Type'].value.to_s == 'text/rfc822-headers' }.body_text | |
# Parse original message headers into a Mail::Message instance | |
original_msg = Mail::Message.new(original_message_headers) | |
# Original message Headers | |
Hash[*original_msg.header.fields.flat_map { |f| [f.name.to_s, f.value.to_s] }] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment