Guest User

Untitled

a guest
Aug 2nd, 2018
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1.  
  2. # grab email addresses from your IMAP inbox
  3.  
  4. def getHeadersFromEmails(host = 'imap.gmail.com', username = '', password = '', folder = 'INBOX', header = 'FROM', search = 'ALL'):
  5. import imaplib
  6. import re
  7. imap = imaplib.IMAP4_SSL(host, 993)
  8. imap.login(username, password)
  9. mboxes = imap.list()
  10. n = imap.select(folder)
  11. print "fetching %s messages" % n[1][0]
  12. uids = imap.search(None, search)
  13. uids = uids[1][0].split(' ')
  14. emails = []
  15. for uid in uids:
  16. print "fetch message %s" % uid
  17. body = imap.fetch(uid,'(BODY[HEADER.FIELDS (%s)])' % header)
  18. text = body[1][0][1]
  19. m = re.search(r"From: ([^\r\n]+)", text)
  20. if m:
  21. m2 = re.search(r"<([^>]+)>", m.group(1))
  22. if m2:
  23. emails.append(m2.group(1))
  24. else:
  25. emails.append(m.group(1))
  26. return set(emails)
  27.  
  28.  
  29. if __name__ == '__main__':
  30.  
  31. EMAIL = 'xxxxx@revolunet.com'
  32. PASSWORD = 'xxxxxxx'
  33.  
  34. emails = getHeadersFromEmails(username = EMAIL , password = PASSWORD)
  35.  
  36. print emails
Add Comment
Please, Sign In to add comment