Last active
August 29, 2015 13:56
-
-
Save winny-/9029110 to your computer and use it in GitHub Desktop.
Rough and MIME-ignorant attempt at processing my old Lavabit emails.
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
#!/usr/bin/env python | |
import collections | |
import os | |
def make_email_dict(email): | |
"""Make an email dictionary. | |
Returns | |
{ 'subject': subject, | |
'sender': sender, | |
'recipient': recipient, | |
'body': body, | |
'date': date, | |
} | |
""" | |
email_dict = collections.defaultdict(lambda: '(Header missing)') | |
in_body = False | |
body = [] | |
with open(email, 'r') as f: | |
for line in f: | |
line = line.strip('\r\n') | |
if not in_body: | |
if line == '': | |
in_body = True | |
if line.startswith('Date: '): | |
email_dict['date'] = line.replace('Date: ', '', 1) | |
elif line.startswith('Subject: '): | |
email_dict['subject'] = line.replace('Subject: ', '', 1) | |
elif line.startswith('From: '): | |
email_dict['sender'] = line.replace('From: ', '', 1) | |
elif line.startswith('To: '): | |
email_dict['recipient'] = line.replace('To: ', '', 1) | |
else: | |
body.append(line) | |
email_dict['body'] = '\n'.join(body) | |
return email_dict | |
def email_dicts(dir_): | |
for email in (f for f in os.listdir(dir_) if f.endswith('.eml')): | |
yield make_email_dict(email) | |
def make_html(email_dicts, html=None): | |
if html is None: | |
html = 'email.html' | |
with open(html, 'w') as html: | |
html.write('<head><title>Emails from Lavabit</title></head>\n') | |
html.write('<body>\n') | |
li = lambda l, r: html.write(''.join(['<li>', l, ': ', r, '</li>\n'])) | |
for email in email_dicts: | |
html.write('<article>\n') | |
html.write('<ul>\n') | |
li('Date', email['date']) | |
li('Sender', email['sender']) | |
li('Recipient', email['recipient']) | |
li('Subject', email['subject']) | |
html.write('</ul>\n') | |
html.write(''.join(['<pre>', email['body'], '</pre>\n'])) | |
html.write('</article>\n') | |
html.write('</body>') | |
if __name__ == '__main__': | |
make_html(email_dicts('.')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment