Subscribeimport email
import getpass
import imaplib
HOST = "imap.example.com"
USER = "alice"
FOLDER = "2006/sent/sent"
connection = imaplib.IMAP4_SSL(HOST)
res, data = connection.login(USER, getpass.getpass())
assert res == "OK"
res, count = connection.select(FOLDER)
assert res == "OK"
res, (msg_nums,) = connection.search(None, "ALL")
assert res == "OK"
for msg_num in msg_nums.split():
res, message_text = connection.fetch(msg_num, "(RFC822)")
assert res == "OK"
message = email.message_from_string(message_text[0][1])
tos = message.get_all("To") or []
ccs = message.get_all("Cc") or []
all_recipients = email.Utils.getaddresses(tos + ccs)
print "\n".join(addr.lower() for realname, addr in all_recipients)
You are not logged in, either login or create an account to post comments
grep "^To: " * > emails.txt
Run that in each folder. It should pull the To: lines out of each email file, and store them in emails.txt. Then you can search&replace on emails.txt to get rid of all the "to:" and change the carriage returns to commas.
You could do it more elegantly with awk or perl, but if it's only a few thousand emails, and you only need to run it once, grep's yer buddy.
posted by ParsonWreck at 6:22 AM on June 6, 2006