forked from FutureDays/thm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_email.py
81 lines (77 loc) · 2.47 KB
/
send_email.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#sends email from thm gmail acct
def format_log_for_email(log_path):
'''
formats the log file for email
'''
import re
try:
import pathlib
import time
lines = []
_tmp_log = pathlib.Path(log_path)
tmp_log = _tmp_log.parent / "email_log.txt"
with open(log_path) as log_file:
for line in log_file:
match = ''
match = re.match(r"\d{4}-\d{2}-\d{2}",line)
if match:
if not "DEBUG:" in line:
lines.append(line)
tmp_log.touch(exist_ok=True)
time.sleep(1)
with open(str(tmp_log),"w+") as tlog:
for line in lines:
tlog.write(line)
return str(tmp_log)
except Exception as e:
print(e)
return False
def send_email(subject,message,attachment_path,debug=False):
'''
sends an email using config from video-post-processing
'''
import os
import smtplib
from email.message import EmailMessage
import configparser
'''
init config info from video-post-processing-config
'''
scriptRepo = os.path.dirname(os.path.abspath(__file__))
config = configparser.ConfigParser()
config.read(os.path.join(scriptRepo,"video-post-process-config.txt"))
if not debug:
_recipients = config.get('email','recipientlist')
else:
_recipients = config.get('email','debug')
sender_email = config.get('email','senderaddress')
sender_pwd = config.get('email','senderpwd')
email_server = config.get('email','server')
recipients = _recipients.split(",")
'''
init the email message
'''
msg = EmailMessage()
msg['From'] = sender_email
msg['To'] = ', '.join(recipients)
msg['Subject'] = subject
'''
attachment handler
'''
if attachment_path:
if os.path.getsize(attachment_path) < 9961472:
with open(attachment_path,"rb") as file:
file_data = file.read()
msg.add_attachment(file_data, maintype='text', subtype='plain')
else:
txt = txt + "\n\n attachment too big to include. Find it at the following location: " + attachment
'''
actually send the email
with smtplib.SMTP(email_server) as server:
server.send_message(msg)
'''
smtp = smtplib.SMTP(email_server)
smtp.starttls()
smtp.login(sender_email,sender_pwd)
smtp.sendmail(sender_email,recipients,msg.as_string())
smtp.close()