<?xml version="1.0" encoding="utf-8"?><!DOCTYPE article  PUBLIC '-//OASIS//DTD DocBook XML V4.4//EN'  'http://www.docbook.org/xml/4.4/docbookx.dtd'><article><articleinfo><title>FindAllAddresses</title><revhistory><revision><revnumber>3</revnumber><date>2013-01-16 19:20:33</date><authorinitials>RobinTempleton</authorinitials><revremark>author credit</revremark></revision><revision><revnumber>2</revnumber><date>2013-01-16 06:47:16</date><authorinitials>RobinTempleton</authorinitials><revremark>use getpass to avoid echoing password</revremark></revision><revision><revnumber>1</revnumber><date>2013-01-16 01:06:11</date><authorinitials>c-50-138-36-67.hsd1.fl.comcast.net</authorinitials></revision></revhistory></articleinfo><para>This is a script which will find all To-addresses for a particular domain that exist in your email. </para><para>The purpose is mainly so that people who want to migrate off of having a catch-all for forwarding to Gmail can explicitly set all the addresses they have ever used. </para><para>You need python with IMAPClient installed (I haven't checked if this is true or not on bog). </para><screen><![CDATA[pip install imapclient             # install
pip install --upgrade imapclient   # upgrade]]></screen><para>or </para><screen><![CDATA[easy_install IMAPClient            # install
easy_install --upgrade IMAPClient  # upgrade]]></screen><para>Run the script with </para><screen><![CDATA[python addresses.py $DOMAIN $GMAIL_USERNAME]]></screen><para>replacing the variables with your particular situation.  It can take a very long time to run.  If you want to test that it works reasonably well for your situation, you can find a folder that is small and run the script on that folder (in my case, that's the inbox).  Uncomment the line with 'INBOX' below (and change folder to a smaller one if your inbox is big). </para><para>Let me know if there are any issues. //luna </para><screen><![CDATA[# Written by Daniel Luna <daniel@lunas.se>
from imapclient import IMAPClient
import sys
import re
from getpass import getpass
]]><![CDATA[
domain = sys.argv[1]
HOST = 'imap.gmail.com'
USERNAME = sys.argv[2]
PASSWORD = getpass()
server = IMAPClient(HOST, use_uid=True, ssl=True)
server.login(USERNAME, PASSWORD)
select_info = server.select_folder('[Gmail]/All Mail')
#select_info = server.select_folder('INBOX')
messages = server.search()
response = server.fetch(messages, ['BODY[HEADER.FIELDS (TO)]'])
dompattern = re.compile('@' + domain, flags=re.IGNORECASE)
emails = {}
for msgid, data in response.iteritems():
    x = data['BODY[HEADER.FIELDS (TO)]'].replace('\n', '').replace('\r', '').strip().lower()
    if dompattern.search(x):
        # You may want to extend these character classes
        emails[re.sub('.*[^-a-z0-9_.+]([-a-z0-9_.+]*@' + domain + ').*', '\\1', x)] = True
server.logout()
for email in emails.keys():
    print '%s' % email]]></screen></article>