Skip to content

Instantly share code, notes, and snippets.

@grigb
Last active September 10, 2018 03:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grigb/cd638bdeaa5e3c9ffe82c2ef40acc5af to your computer and use it in GitHub Desktop.
Save grigb/cd638bdeaa5e3c9ffe82c2ef40acc5af to your computer and use it in GitHub Desktop.
Convert an exported Mac Mail mbox file into CSV for data mining
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# assumes your file is called mbox
# writes to a file called mbox.csv
import mailbox
import csv
# handle recursive payloads
# this only exports the human-readable text/plain payload
def more_payloads(message):
body = ""
if message.is_multipart():
for payload in message.get_payload():
body += more_payloads(payload)
else:
if message.get_content_type() == 'text/plain':
body = message.get_payload(decode=True)
return body
with open("mbox.csv", "wb") as outfile:
writer = csv.writer(outfile)
for message in mailbox.mbox('mbox'): ## 'mbox' needs to match the file you're reading from
body = more_payloads(message)
writer.writerow([ message['date'], message['from'], message['subject'], body])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment