Last active
November 7, 2016 21:39
-
-
Save spaghetti-/d61051dd241d079f09da45cebdcaafa2 to your computer and use it in GitHub Desktop.
script that parses out all email addresses one has sent emails to and received email from
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python2.7 | |
| from __future__ import print_function | |
| # some stuff that we want to use doesn't work with python3 + windows | |
| import sys | |
| import yaml | |
| import imaplib | |
| from email.parser import HeaderParser | |
| def update_progress(progress): | |
| sys.stdout.write('\r[{0}{1}] {2}%'.format('#' * (progress/10), | |
| ' ' * (10 - (progress/10)), | |
| progress)) | |
| sys.stdout.flush() | |
| def get_sent_emails(mailbox): | |
| sent = set() | |
| print('[*] selecting mailbox: [Gmail]/Sent Mail') | |
| mailbox.select('[Gmail]/Sent Mail') | |
| _, emails = mailbox.search(None, 'ALL') | |
| emails = map(int, emails[0].split(' ')) | |
| parser = HeaderParser() | |
| c = 0 | |
| print('[*] parsing headers, this may take a while..') | |
| for mail in emails: | |
| c = c + 1 | |
| update_progress(int((c / float(len(emails))) * 100)) | |
| header = parser.parsestr( | |
| mailbox.fetch(mail, '(BODY[HEADER.FIELDS (TO)])')[1][0][1]) | |
| map(sent.add, map(lambda s: s.strip().replace('\r', '').replace('\n', ''), | |
| header.items()[0][1].split(','))) | |
| print() | |
| print('[*] writing to file: sent.csv') | |
| with open('sent_to.csv', 'w') as f: | |
| for email in sent: | |
| print(email, file=f) | |
| def get_recv_emails(mailbox): | |
| sent = set() | |
| print('[*] selecting mailbox: INBOX') | |
| mailbox.select('INBOX') | |
| _, emails = mailbox.search(None, 'ALL') | |
| emails = map(int, emails[0].split(' ')) | |
| parser = HeaderParser() | |
| c = 0 | |
| print('[*] parsing headers, this may take a while..') | |
| for mail in emails: | |
| c = c + 1 | |
| update_progress(int((c / float(len(emails))) * 100)) | |
| header = parser.parsestr( | |
| mailbox.fetch(mail, '(BODY[HEADER.FIELDS (FROM)])')[1][0][1]) | |
| map(sent.add, map(lambda s: s.strip().replace('\r', '').replace('\n', ''), | |
| header.items()[0][1].split(','))) | |
| print() | |
| print('[*] writing to file: sent.csv') | |
| with open('recieved_mail_from.csv', 'w') as f: | |
| for email in sent: | |
| print(email, file=f) | |
| if __name__ == '__main__': | |
| user_details = None | |
| with open('user.yml', 'r') as u: | |
| user_details = yaml.load(u) | |
| try: | |
| M = imaplib.IMAP4_SSL('imap.gmail.com') | |
| print('[*] connecting..') | |
| rv, data = M.login( | |
| user_details['email'], user_details['app_password'] | |
| ) | |
| get_sent_emails(M) | |
| get_recv_emails(M) | |
| except Exception as e: | |
| print('[!] error: {}'.format(str(e))) | |
| finally: | |
| M.logout() | |
| M.close() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| email: [email protected] | |
| app_password: asd |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment