-
Notifications
You must be signed in to change notification settings - Fork 4
/
util.py
45 lines (34 loc) · 1.12 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from email.mime.text import MIMEText
import smtplib
def read_file(path):
with open(path, 'rb') as file_handle:
content = file_handle.read()
return content.decode()
def write_file(str_, path):
with open(path, 'wb') as file_handle:
file_handle.write(str_.encode())
def folder_name_from_url(url):
folder_name = url.rstrip('/')
folder_name = folder_name.replace('http://', '').replace('https://', '')
folder_name = folder_name.replace('/', '_')
folder_name = folder_name.replace('?', '_')
folder_name = folder_name.replace('&', '_')
folder_name = folder_name.replace(':', '_')
return folder_name
def email_oneself(msg,
gmail,
gmail_pswd_path='./gmail_app_pswd',
subject='Untitled'):
pswd = read_file(gmail_pswd_path)
pswd = pswd.strip()
to_emails = [gmail]
from_email = gmail
msg = MIMEText(msg)
msg['Subject'] = subject
msg['To'] = ', '.join(to_emails)
msg['From'] = from_email
mail = smtplib.SMTP('smtp.gmail.com', 587)
mail.starttls()
mail.login(gmail, pswd)
mail.sendmail(from_email, to_emails, msg.as_string())
mail.quit()