Created
December 16, 2009 16:23
-
-
Save maxp/257958 to your computer and use it in GitHub Desktop.
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
| fp = open(msgfile) | |
| msg = email.message_from_file(fp) | |
| fp.close() | |
| counter = 1 | |
| for part in msg.walk(): | |
| # multipart/* are just containers | |
| if part.get_content_maintype() == 'multipart': | |
| continue | |
| # Applications should really sanitize the given filename so that an | |
| # email message can't be used to overwrite important files | |
| filename = part.get_filename() | |
| if not filename: | |
| ext = mimetypes.guess_extension(part.get_content_type()) | |
| if not ext: | |
| # Use a generic bag-of-bits extension | |
| ext = '.bin' | |
| filename = 'part-%03d%s' % (counter, ext) | |
| counter += 1 | |
| fp = open(os.path.join(opts.directory, filename), 'wb') | |
| fp.write(part.get_payload(decode=True)) | |
| fp.close() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment