Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # grab email addresses from your IMAP inbox
- def getHeadersFromEmails(host = 'imap.gmail.com', username = '', password = '', folder = 'INBOX', header = 'FROM', search = 'ALL'):
- import imaplib
- import re
- imap = imaplib.IMAP4_SSL(host, 993)
- imap.login(username, password)
- mboxes = imap.list()
- n = imap.select(folder)
- print "fetching %s messages" % n[1][0]
- uids = imap.search(None, search)
- uids = uids[1][0].split(' ')
- emails = []
- for uid in uids:
- print "fetch message %s" % uid
- body = imap.fetch(uid,'(BODY[HEADER.FIELDS (%s)])' % header)
- text = body[1][0][1]
- m = re.search(r"From: ([^\r\n]+)", text)
- if m:
- m2 = re.search(r"<([^>]+)>", m.group(1))
- if m2:
- emails.append(m2.group(1))
- else:
- emails.append(m.group(1))
- return set(emails)
- if __name__ == '__main__':
- EMAIL = 'xxxxx@revolunet.com'
- PASSWORD = 'xxxxxxx'
- emails = getHeadersFromEmails(username = EMAIL , password = PASSWORD)
- print emails
Add Comment
Please, Sign In to add comment