Skip to content

Instantly share code, notes, and snippets.

@vitorio
Created March 30, 2015 01:17
Show Gist options
  • Save vitorio/a6c63a10af8fc8968278 to your computer and use it in GitHub Desktop.
Save vitorio/a6c63a10af8fc8968278 to your computer and use it in GitHub Desktop.
So, let's say you have an mbox full of mail and you want to get all the To: addresses out of it. And let's say you've seen a bunch of intimidating complicated examples like http://stackoverflow.com/questions/7166922/extracting-the-body-of-an-email-from-mbox-file-decoding-it-to-plain-text-regard or http://nbviewer.ipython.org/github/furukama/Mini…
# via http://stackoverflow.com/questions/14903664/determine-unique-from-email-addresses-in-maildir-folder
import mailbox
import email
mbox = mailbox.mbox('DADEOL/AOL Mail sorted/Saved.DADEOL Sent.mbox')
uniq_emails = set(email.utils.parseaddr(msg['to'])[1].lower() for msg in mbox)
for a in uniq_emails:
print a
print len(uniq_emails)
@theojepsen
Copy link

Thank you for that example! I made a few changes:

  • Open a Maildir instead of mbox
  • Get "From", "To" and "Cc" addresses
  • Print the unique names instead of addresses
#!/usr/bin/env python3
import mailbox
import email

maildir = mailbox.Maildir('path/to/my/maildir/All Mail/')

uniqNames = set(addr[0] for msg in maildir for addr in email.utils.getaddresses([msg['from'] + (msg['to'] or '') + (msg['cc'] or '')]))

for f in uniqNames: print(f)

@vitorio
Copy link
Author

vitorio commented Jan 30, 2022

@theojepsen glad it was useful! Thanks for providing an additional one.

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