MIME Decoding of a screwed up email message

On a server I run for a customer, I have MailScanner (don’t flame me.. it works!) configure to parse their incoming email.

Anyway, one of their messages got clobbered by MailScanner and rather than quarantining the attachments as it normally does, it quarantined the whole message, and they needed the file from within the message.

The file was base64 encoded within the ASCII body of the message.


Basically, this is a one-line (plus the necessary bits) Python script to fix the problem. Save a copy of the message and remove all the “email” bits from around the base64 encoded content, including the boundary lines, so you are left with just the base64 encoded lines.

Then, put the following into a text file:
import sys, base64
base64.decode(open(sys.argv[1], 'rb'), open(sys.argv[2], 'wb'))

Save the file as something like convert.py

Then, you just need to go
python convert.py inputfilename outputfilename

Depending on the machine and the size of the base64 attachment, it should take a couple of seconds and then you have the file!