welcome: please sign in

Diff for "FindAllAddresses"

Differences between revisions 1 and 2
Revision 1 as of 2013-01-16 01:06:11
Size: 2189
Editor: Luna
Comment:
Revision 2 as of 2013-01-16 06:47:16
Size: 1999
Comment: use getpass to avoid echoing password
Deletions are marked like this. Additions are marked like this.
Line 23: Line 23:
I don't really know Python so I'm inadvertently echoing when you input the password. Let me know if there are any issues. //luna Let me know if there are any issues. //luna
Line 28: Line 29:
from getpass import getpass
Line 32: Line 34:
## I don't know Python well enough to not echo your input (this also
## ruins pipes for this script. Sorry about that.
PASSWORD = raw_input('password:')
PASSWORD = getpass()

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

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

FindAllAddresses (last edited 2013-01-16 19:20:33 by RobinTempleton)