Skip to content

Instantly share code, notes, and snippets.

@temochka
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save temochka/22399567b4012eb7760d to your computer and use it in GitHub Desktop.
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
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"}
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