I was looking for a script to extract eml attachments from mails on my mailboxserver. After looking for 5min I found I might get faster if I write it myself and choose python because it already has email message parsers on board.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import re import email import hashlib from os import walk # dir to save extracted files outdir = '/path/to/folder' # path to folder containing mailmessages sourcedir = '/home/myuser/.Maildir/.INBOX.Ham/cur' f = [] r = re.compile("^[0-9]+.*$") for (dirpath, dirnames, filenames) in walk(sourcedir): f.extend(filter(r.match, filenames)) for file in f: file = sourcedir + '/' + file hash = hashlib.sha256(file).hexdigest() with open(file) as fl: msg = email.message_from_file(fl) for part in msg.walk(): if part.get_content_type() == "application/octet-stream": with open(outdir + '/' + hash + '.eml', 'w') as fl: fl.write(part.get_payload(decode=True).replace('\r', "\r\n")) |
Leave a Reply