This is a script which will find all To-addresses for a particular domain that exist in your email.
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.
You need python with IMAPClient installed (I haven't checked if this is true or not on bog).
pip install imapclient # install pip install --upgrade imapclient # upgrade
or
easy_install IMAPClient # install easy_install --upgrade IMAPClient # upgrade
Run the script with
python addresses.py $DOMAIN $GMAIL_USERNAME
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).
Let me know if there are any issues. //luna
# Written by Daniel Luna <daniel@lunas.se> from imapclient import IMAPClient import sys import re from getpass import getpass 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