Skip to content

Instantly share code, notes, and snippets.

@brianboyer
Created November 6, 2014 14:57
Show Gist options
  • Save brianboyer/c9d412a26cb57b5ebbc7 to your computer and use it in GitHub Desktop.
Save brianboyer/c9d412a26cb57b5ebbc7 to your computer and use it in GitHub Desktop.
Convert a mailbox file into CSV
# assumes your file is called mbox (which it is if you export from Mac Mail)
# writes to a file called mbox.csv
import mailbox
import csv
# motherfucking recursion, because email is damn weird.
# each payload can contain many other payloads, which can contain many *other* payloads
# this only exports the text/plain payload, the thing you read
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", "w") as outfile:
writer = csv.writer(outfile)
for message in mailbox.mbox('mbox'):
body = more_payloads(message)
writer.writerow([message['subject'], message['from'], message['date'], body])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment