Skip to content

Instantly share code, notes, and snippets.

@terrettaz
Created February 12, 2011 10:23
Show Gist options
  • Save terrettaz/823674 to your computer and use it in GitHub Desktop.
Save terrettaz/823674 to your computer and use it in GitHub Desktop.
Run this script on a Mac to find all email addresses of persons who wrote emails to a specific email address.
#!/usr/bin/python
# -*- coding: iso-8859-1 -*-
__version__ = "$Revision: 0.1 $"
__author__ = "Pierrick Terrettaz"
__date__ = "2008-05-08"
import popen2
import re
import sys
import os
if __name__ == '__main__':
prog = os.path.basename(sys.argv[0])
if len(sys.argv) < 2:
print '== Find all sender addresses in your mailboxes =='
print 'Usage: ' + prog + ' emailTo [-onlyin path]'
print ' emailTo : your email address'
print ' -onlyin : find onlyin a certain folder scope'
print ' (for more information see mdfind help)'
print 'Requires : Mac OSX platform with mdfind, mdls programs'
print '\n Example : '+prog+' [email protected] -onlyin /Users/pik/Library/Mail/Mailboxes'
sys.exit(1)
onlyin = ''
if len(sys.argv) >= 4 and sys.argv[2] == '-onlyin':
onlyin = '-onlyin "%s"' % sys.argv[3]
re_email = re.compile('.*"(.+)".*')
addresses = {}
mdfind_cmd = 'mdfind '+onlyin+' "(kMDItemRecipientEmailAddresses == \''+sys.argv[1]+'\')"'
(stdout, stdin) = popen2.popen2(mdfind_cmd)
for path in stdout.xreadlines():
path = path[:-1]
(stdout2, stdin2) = popen2.popen2('mdls -name kMDItemAuthorEmailAddresses -raw "'+path+'"')
for line in stdout2.xreadlines():
line = line[:-1]
res = re_email.findall(line)
if len(res) == 1:
address = res[0]
if address not in addresses:
addresses[address] = 0
addresses[address] += 1
rev_items = [(v, k) for k, v in addresses.items()]
rev_items.sort()
for v, k in rev_items:
print "%d\t: %s" % (v, k)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment