From c27abf74f4b16c5ef3c7f64b4dc93780452e982e Mon Sep 17 00:00:00 2001 From: shalini-s Date: Sun, 13 Oct 2019 16:21:02 -0400 Subject: [PATCH 1/2] Added save_emails script --- save_emails/README.md | 7 ++++ save_emails/save_emails.py | 73 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 save_emails/README.md create mode 100644 save_emails/save_emails.py diff --git a/save_emails/README.md b/save_emails/README.md new file mode 100644 index 0000000..50d7ead --- /dev/null +++ b/save_emails/README.md @@ -0,0 +1,7 @@ +# A simple script to read in all unread emails from an inbox +# and save the text to a local directory - i use this for sending myself notes and saving them to a local directory! +# You can also use this code to parse any email accounts you have to track trends or save specific emails! + +# Requirements: +- Config file with email address and password +- Gmail account with third party access turned on diff --git a/save_emails/save_emails.py b/save_emails/save_emails.py new file mode 100644 index 0000000..dca952f --- /dev/null +++ b/save_emails/save_emails.py @@ -0,0 +1,73 @@ +import imaplib +import email +from collections import defaultdict +import configparser +import os +import sys +from os import listdir +from os.path import isfile, join + +# adds email note to corresponding note in directory +def add_to_note(to_add): + dir_1 = "../thoughts_on/" + for new_note in to_add: + temp_file = open(dir_1+'temp.txt',"w+") + new_txt = to_add[new_note][0] + '\n' + to_add[new_note][1] + '\n' + temp_file.write(new_txt) + print("wrote %s" % new_txt) + fname = dir_1 + new_note + '.txt' + + # adds new note to beginning of file + old_file = open(fname, "r+") + temp_file.write(old_file.read()) + old_file.seek(0) + temp_file.seek(0) + old_file.write(temp_file.read()) + + temp_file.close() + old_file.close() + +# read email +def readmail(): + notes_to_add=defaultdict(lambda:'') + + config=configparser.RawConfigParser() + config.read(os.path.join(os.path.dirname(sys.argv[0]),".emailrc")) + email_address = config.get('auth','email') + password = config.get('auth','password') + approved_from = config.get('auth','from') + + SMTP_SERVER = "imap.gmail.com" + SMTP_PORT = 993 + mail = imaplib.IMAP4_SSL(SMTP_SERVER, SMTP_PORT) + mail.login(email_address,password) + mail.select('inbox') + + # reads unseen messages and adds to dict + type, data = mail.search(None, '(UNSEEN)') + mail_ids = [int(x) for x in data[0].decode("utf-8").split()] + for index, emailid in enumerate(mail_ids): + resp, data = mail.fetch(str(emailid),'(RFC822)' ) + for response_part in data: + if isinstance(response_part, tuple): + msg = email.message_from_string(response_part[1].decode('utf-8')) + # print(msg.keys()) + email_subject = msg['subject'] + email_from = msg['from'] + email_date = msg['date'] + for part in msg.walk(): + if part.get_content_type() == 'text/plain': + body = part.get_payload() # prints the raw text + # if email_from == approved_from: + notes_to_add[email_subject] = [email_date,body] + + mail.close() + mail.logout() + + return notes_to_add + +if __name__ == '__main__': + to_add = readmail() + + add_to_note(to_add) + From 2f553098c13c8c795e2de4eb3213634d34acc59d Mon Sep 17 00:00:00 2001 From: shalini-s Date: Tue, 29 Oct 2019 18:36:42 -0400 Subject: [PATCH 2/2] updated README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 83c7fdb..2786acc 100644 --- a/README.md +++ b/README.md @@ -15,8 +15,8 @@ A list of super awesome scripts which help automate the boring tasks. | [arch_linux](https://github.com/adityaarakeri/super-scripts/tree/master/arch_linux) | [crouther](https://github.com/crouther) | [wifi_to_eth](https://github.com/adityaarakeri/super-scripts/tree/master/wifi_to_eth) | [crouther](https://github.com/crouther) |[file_finder](https://github.com/adityaarakeri/super-scripts/tree/master/file_finder) | [poszy](https://github.com/poszy) | -| [makere](https://github.com/adityaarakeri/super-scripts/tree/master/makere) | [aksJain0](https://github.com/aksJain0) - +| [makere](https://github.com/adityaarakeri/super-scripts/tree/master/makere) | [aksJain0](https://github.com/aksJain0) | +| [save_emails](https://github.com/adityaarakeri/super-scripts/tree/master/save_emails) | [shalini-s](https://github.com/shalini-s) | ## Contribuition Guidelines - Make a **separate folder** for your script. - The script can be in any language