-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathemail_alert.py
63 lines (55 loc) · 2.34 KB
/
email_alert.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
#!/usr/bin/python3
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
import os
import base64
from prettytable import PrettyTable
class EmailAction:
"""Class to send an email alert of new results."""
def __init__(self, e_from, e_to, e_domain, e_port, e_pw):
"""Initialize class and set variables."""
self.e_from = e_from
self.e_to = e_to
self.e_domain = e_domain
self.e_port = e_port
self.e_pw = e_pw
def send_alert(self, full_path, num_results, email_body):
"""Construct email message and send."""
message = MIMEMultipart()
e_from = self.e_from
e_to = self.e_to
self.e_pw = self.e_pw[2:-1]
password = base64.b64decode(self.e_pw)
password = str(password, 'utf-8')
f_name = os.path.basename(full_path)
f_query = f_name.split("--")[2].replace('-','.').replace(".zip","")
f_time = f_name.split("--")[1].replace("-",":")
message['From'] = e_from
message['To'] = e_to
message['Subject'] = "There are " + str(num_results) + " results for query " + f_query + " at " + f_time
body = 'Results for query: ' + f_query + '\n\n'
tbl = PrettyTable(header_style='upper', padding_width=10,
field_names=["Repo Author", "Repo Name", "Repo File", "Line - first 64 chars"])
for item in email_body:
file_match = os.path.basename(item["Path"])
tbl.add_row([item["Repo Author"], item["Repo Name"], file_match, item["Line"].strip()[:68]])
body += str(tbl)
message.attach(MIMEText(body, 'plain'))
attachment = open(full_path, "rb")
payload = MIMEBase('application', 'octet-stream')
payload.set_payload((attachment).read())
encoders.encode_base64(payload)
payload.add_header('Content-Disposition', "attachment; filename= %s" % f_name)
message.attach(payload)
send = smtplib.SMTP(self.e_domain, self.e_port)
send.starttls()
send.login(e_from, password)
text = message.as_string()
send.sendmail(e_from, e_to, text)
send.quit()
if __name__ == "__main__":
a = EmailAction()
a.send_alert()