forked from Buffer94/NetHound
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogSender.py
126 lines (111 loc) · 4.61 KB
/
LogSender.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import smtplib
import json
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
class LogSender:
"""
A class useful to send email address with or without attachments
----------
user : str
The email address used to send mail
password : str
The password of the account
server_address : str
The address of the server SMTP
server_port : int
The port on which the server is listening
Methods
-------
send(addresses, body, subject, attachment, att_type, fname)
A method that sends an email with the possibility to add an attachment taken from a string or a file
"""
def __init__(self):
self.user = '[email protected]'
with open('email.naspy', 'r') as credentials:
data = json.loads(credentials.read())
self.password = data["password"]
self.addresses = data["addresses"]
self.server_address = 'smtp.gmail.com'
self.server_port = 465
def send(self, body, subject, addresses=None, attachment=None, att_type=None, fname='attachment'):
"""
A method that sends an email with the possibility to add an attachment taken from a string or a file
Parameters
----------
body:str
The body of the mail
subject:str
The subject of the mail
addresses:list(str)
The list of addresses to check
attachment:list(str)
A facultative field used to pass an attachment as string or a series of attachments
att_type:list(str)
A facultative field used to discriminate the nature of the attachments. It could be a string or a list of string
fname:list(str)
The name to give at the attachment(s). In case it is not passed it takes the name attachment
"""
msg = MIMEMultipart('alternative')
msg['Subject'] = subject
msg['From'] = self.user
if addresses is None:
addresses = self.addresses
if isinstance(addresses, str):
msg['To'] = addresses
else:
msg['To'] = ', '.join(addresses)
content = MIMEText(body, 'plain')
msg.attach(content)
if attachment is not None:
if type(attachment) == list:
for i in range(len(attachment)):
if att_type[i] is None:
att_type[i] = 'filename'
if att_type[i] == 'filename':
with open(attachment[i], 'r') as file:
payload = MIMEText(file.read())
filename = attachment[i]
elif att_type[i] == 'text':
payload = MIMEText(attachment[i])
filename = '%s.txt' % fname[i]
elif att_type[i] == 'json':
payload = MIMEText(attachment[i])
filename = '%s.json' % fname[i]
else:
print('Error, attachment must be text, json or filename')
return
payload.add_header('Content-Disposition', 'attachment', filename=filename)
msg.attach(payload)
else:
if att_type is None:
att_type = 'filename'
if att_type == 'filename':
with open(attachment, 'r') as file:
payload = MIMEText(file.read())
filename = attachment
elif att_type == 'text':
payload = MIMEText(attachment)
filename = '%s.txt' % fname
elif att_type == 'json':
payload = MIMEText(attachment)
filename = '%s.json' % fname
else:
print('Error, attachment must be text, json or filename')
return False
payload.add_header('Content-Disposition', 'attachment', filename=filename)
msg.attach(payload)
num_attempt = 0
while num_attempt < 10:
try:
server = smtplib.SMTP_SSL(self.server_address, self.server_port)
server.ehlo()
server.login(self.user, self.password)
server.sendmail(self.user, addresses, msg.as_string())
server.close()
return True
except Exception:
if num_attempt >= 10:
print('Something went wrong...')
return False
num_attempt += 1
print('Error, retrying')