Skip to content

Instantly share code, notes, and snippets.

@nfelger
Created February 4, 2015 12:08
Show Gist options
  • Save nfelger/21b690099e0e68ad5f83 to your computer and use it in GitHub Desktop.
Save nfelger/21b690099e0e68ad5f83 to your computer and use it in GitHub Desktop.
Get all domains an mbox has received email from
import mailbox
from collections import defaultdict
from flanker import mime
from flanker.addresslib import address
mbox_path = '...'
mbox = mailbox.mbox(mbox_path)
domains = defaultdict(int)
items = mbox.iteritems()
for msg in items:
raw_msg = msg[1].as_string()
parsed = mime.from_string(raw_msg)
from_header = parsed.headers['From']
from_domain = address.parse(from_header).hostname
domains[from_domain] += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment