Skip to content

Instantly share code, notes, and snippets.

@tyndyll
Created January 23, 2015 18:49
Show Gist options
  • Save tyndyll/6f6145f8b1e82d8b0ad8 to your computer and use it in GitHub Desktop.
Save tyndyll/6f6145f8b1e82d8b0ad8 to your computer and use it in GitHub Desktop.
Parsing Maildir Mail Stored on Disk with Python
def unicode_it(data):
# Take a string of data and convert it to unicode
try:
return unicode(data, errors="replace").strip()
except TypeError as E:
return u""
def extract(msg, msg_obj, attachments):
if msg.is_multipart():
for part in msg.get_payload():
if part.is_multipart():
extract(part, msg_obj, attachments)
continue
if part.get('Content-Disposition') is None:
msg_obj["body"] += unicode_it(part.get_payload())
else:
if part.get('Content-Disposition').startswith('attachment'):
attachments[part.get_filename()] = {
'data': part.get_payload(decode=True),
'mime': part.get_content_type()
}
else:
msg_obj["body"] += unicode_it(msg.get_payload())
return
mail_directory = "maildir"
mdir = mailbox.Maildir(mail_directory, factory=email.message_from_file)
for directory in mdir.list_folders():
d = mdir.get_folder(directory)
for msg in d:
msg_obj = {"path": directory, "body": ""}
attachments = {}
for key, value in msg.items():
msg_obj[key.lower()] = unicode_it(value)
lists = ["from", "cc", "bcc", "to"]
extract(msg, msg_obj, attachments)
# Add your own handler to do things with the message
# if attachments:
# for fname, data in attachments.items():
# Add your own handler to do things with the attachments
@crossmax
Copy link

Hi @tyndyll
Thanks for this code.
I'm looking for some code like this to extract attachments but I'm getting a error when I run it.
Traceback (most recent call last): File "extract.py", line 28, in <module> mdir = mailbox.Maildir(mail_directory, factory=email.message_from_file) NameError: name 'mailbox' is not defined
Do you have missed some line when uploaded the code to github?

@JaDogg
Copy link

JaDogg commented Dec 3, 2018

@crossmax you need to import relevant classes. Try opening it in pycharm. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment