Skip to content

Added save_emails script #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ 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) |
|[loop_command](https://github.com/adityaarakeri/super-scripts/tree/master/loop_command) | [jeancsil](https://github.com/jeancsil)
| [autofill_information](https://github.com/adityaarakeri/super-scripts/tree/master/autofill_information) | [pcube99](https://github.com/pcube99)
|[raw_to_jpeg](https://github.com/adityaarakeri/super-scripts/tree/master/raw_to_jpeg) | [Sadeed](https://github.com/Sadeed)
Expand All @@ -39,7 +39,7 @@ A list of super awesome scripts which help automate the boring tasks.
| [bootstrap_mac_dev_env](https://github.com/dirtyonekanobi/super-scripts/tree/bootstrap-dev/bootstrap_mac_dev_env) | [dirtyoneanobi](https://github.com/dirtyonekanobi) |
| [linux_lock_with_usb_protection](https://github.com/Anarcroth/super-scripts/tree/feature/add-linux-lock-with-usb-protection) | [Anarcroth](https://github.com/Anarcroth) |
| [mouse_keyboard_automator](https://github.com/adityaarakeri/super-scripts/tree/master/mouse_keyboard_automator) | [Rafed](https://github.com/rafed123) |

| [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.
Expand Down
7 changes: 7 additions & 0 deletions save_emails/README.md
Original file line number Diff line number Diff line change
@@ -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
73 changes: 73 additions & 0 deletions save_emails/save_emails.py
Original file line number Diff line number Diff line change
@@ -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)