-
Notifications
You must be signed in to change notification settings - Fork 118
/
email_try.py
75 lines (59 loc) · 2.09 KB
/
email_try.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
from email.mime.application import MIMEApplication
from html_try import msg_main
import os
import yaml
import smtplib
from os.path import isfile, exists
from datetime import datetime
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email import encoders
from utilities.make_domains_list import domain_list
def attach_file_to_email(email_message, filename, extra_headers=None):
with open(filename, "rb") as f:
file_attachment = MIMEApplication(f.read())
file_attachment.add_header(
"Content-Disposition",
f"attachment; filename= {filename}",
)
if extra_headers is not None:
for name, value in extra_headers.items():
file_attachment.add_header(name, value)
email_message.attach(file_attachment)
def send_alert_mail():
subject = 'RTA Tool Results '
msg = msg_main()
#msg = "hi"
path = os.path.dirname(os.path.abspath(__file__))
with open(path + "/config_mail", "r") as ymlfile:
config = yaml.safe_load(ymlfile)
To = config['sendto']['email']
#for email,value in config['sendto'].items():
#To.append(str(value))
#print(To)
Username = config['email']['username']
Password = config['email']['password']
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(Username, Password)
date = str(datetime.now()).split('.')[0]
mssg = MIMEMultipart('alternative')
mssg['Subject'] = subject + " - " + date
mssg['From'] = Username
mssg['To'] = ", ".join(To)
html = MIMEText(msg,'html')
domains = domain_list("./input_files/domains.txt")
mssg.attach(html)
l = ["./"+domain+"/tools/Port_scan/pie_chart.png" for domain in domains]
for i in l:
attach_file_to_email(mssg, i, {'Content-ID':"<"+i+">"})
try:
sent = server.sendmail(Username, To, mssg.as_string())
server.quit()
print('[>] ['+date+'] Alert mail sent')
return True
except Exception as ae:
print(ae)
print('[!] ['+date+'] Alert mail failed to send')
return False
#send_alert_mail()