0

We audit our outgoing emails, so that later on we can access the raw email text. For emails that had attachments, I can see a mimepart section like this:

--mimepart_5a65f317c19d2_24515571f0306fc12617a
Content-Type: application/pdf; name=CMI-1Y-120628-38895.pdf
Content-Transfer-Encoding: Base64
Content-Disposition: attachment; filename=CMI-1Y-120628-38895.pdf

JVBERi0xLjQKJeLjz9MKMyAwIG9iaiA8PC9UeXBlL1hPYmplY3QvQ29sb3JT
cGFjZS9EZXZpY2VHcmF5L1N1YnR5cGUvSW1hZ2UvQml0c1BlckNvbXBvbmVu
dCA4L1dpZHRoIDMwMC9MZW5ndGggNTI3My9IZWlnaHQgMjYwL0ZpbHRlci9G
bGF0ZURlY29kZT4+c3RyZWFtCnic7Z15YA3XGsC/EEFQKrb2WVISUi2JrbFU
aWyxvKKx11LUUlu1SCyhPPqaerQo2nqW1kMVr9UnfUijqZ3aW2KLUGt5QcQS
JDLvzjkz986ZOffec+XefHMbvz/izsw5kzM/NzNnzvIdADdS76PVI1qUducZ

....about 800 lines of text like this...

MCBuIAowMDAwMDM4MjczIDAwMDAwIG4gCjAwMDAwMzgzMTggMDAwMDAgbiAK
dHJhaWxlcgo8PC9Sb290IDEyIDAgUi9JRCBbPGIyNjEwYmQyZmM3ZGQyZTI0
NzQxYzg2M2VkOWU4NTBkPjwxY2JiNDIzYWZmMWVjY2U2MGRhNmJjMmE4ZjZl
MTBjNT5dL0luZm8gMTMgMCBSL1NpemUgMTQ+PgpzdGFydHhyZWYKMzg0NTUK
JSVFT0YK

--mimepart_5a65f317c19d2_24515571f0306fc12617a

What I want to do is to give the admin looking at this email record the option to "reconstruct" the attachment. So, when they click the "View attachment" button, i would use the data to build a file (with the filename from the headers above), then serve that back to them.

The question is, how to turn this into a file? The attachments in question are always PDF files, so if there's a solution which only works with pdfs then that's fine, but a more general solution would be preferred.

I'm using Ruby on Rails, so there might be a function in ActiveMailer already to do this...

EDIT: In case anyone wants to try with some real data, I made a PDF which just has a bit of text in large letters, and attached it to an email. The link below is to a text file with all the raw data from that email, with text/plain, text/html and application/pdf parts. Your challenge is to turn the application/pdf mimepart into a pdf file, and tell me what it says (and more importantly, post an answer saying what you did). thanks!

https://www.dropbox.com/s/u3kt5sdl6b44fms/raw_email.txt?dl=0

Max Williams
  • 32,435
  • 31
  • 130
  • 197

1 Answers1

0

This turned out to be easier than I thought.

Algorithmically:

  • get the block of text from that mimepart (ie without the mimepart boundaries)
  • base64 decode it
  • write the result of that out to filename.pdf

So obvious that I didn't even try it!

In Ruby, specifically: I'm sure you could do it much more tersely and safely than this.

text = File.read("/home/max/raw_email.txt")
boundary_line = text.grep(/boundary\=mimepart/).first.chomp
mimepart_divider = "--#{boundary_line.split("boundary=").last}"
mimeparts = text.split(mimepart_divider)
mimepart = mimeparts.grep(/application\/pdf/).first
filename = mimepart.grep(/filename\=/).first.chomp.split("filename=").last
mimedata = mimepart.split("\n\n").compact_blank.last
decoded = Base64.decode64(mimedata)
File.open("#{ENV['HOME']}/#{filename}","w"){|f| f.puts decoded}
Max Williams
  • 32,435
  • 31
  • 130
  • 197