-
Notifications
You must be signed in to change notification settings - Fork 2
/
send_email_alert.py
executable file
·149 lines (129 loc) · 4.77 KB
/
send_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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/python3
"""Send an email alert saying that the bluish pink server is down."""
import sys
import os
import json
import ssl
import smtplib
from datetime import datetime
from collections import namedtuple
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
class SendEmailAlert:
def __init__(
self,
configFile: str = None,
mailFrom: str = None,
mailTo: str = None,
host: str = None,
port: int = None,
user: str = None,
password: str = None,
subject: str = '',
messagePrefix: str = '',
messageSuffix: str = '',
):
"""
Sets the paramaters for sending the email and creates the email to be
sent.
Args:
configFile - (str) Path to a JSON file containing the email params.
mailFrom - (str) The email account from which the email should be
sent from.
mailTo - (str) Recipient email address.
host - (str) Email host.
port - (int) Email port.
user - (str) Email User.
password - (str) Email password.
subject - (str) Email subject.
messagePrefix - (str) Text to be prepended to the to email body.
messageSuffix - (str) Text to be appended to the email body.
"""
self._Parms = self._set_params(configFile, mailFrom, mailTo, host,
port, user, password)
self._msg = self._set_msg(subject, messagePrefix, messageSuffix)
@staticmethod
def _set_params(
configFile: str = None,
mailFrom: str = None,
mailTo: str = None,
host: str = None,
port: int = None,
user: str = None,
password: str = None
) -> namedtuple:
"""Sets the email connection parameters including the email address of
the recipient.
Args:
configFile - (str) Path to a JSON file containing the email params.
mailFrom - (str) The email account from which the email should be
sent from.
mailTo - (str) Recipient email address.
host - (str) Email host.
port - (int) Email port.
user - (str) Email User.
password - (str) Email password.
Returns:
namedtuple - Collection of the email connection parameters.
"""
connParams = {}
if configFile:
with open(configFile, 'r') as jsonFile:
connParams = json.load(jsonFile)
if mailFrom:
connParams['FROM'] = mailFrom
if mailTo:
connParams['TO'] = mailTo
if host:
connParams['HOST'] = host
if port:
connParams['PORT'] = port
if user:
connParams['USER'] = user
if password:
connParams['PASSWORD'] = password
return namedtuple('connectionParams', connParams)(**connParams)
def _set_msg(self, subject: str, messagePrefix: str, messageSuffix: str):
"""Creates the message object to be sent.
Args:
subject - (str) Email subject.
messagePrefix - (str) Text to be prepended to the to email body.
messageSuffix - (str) Text to be appended to the email body.
"""
msg = MIMEMultipart("alternative")
msg['Subject'] = subject
msg['From'] = self.get_params().FROM
msg['To'] = self.get_params().TO
msg.attach(
MIMEText(
f'PINGING THE SERVER FAILED AT {datetime.now()}',
'plain')
)
msgBody = f"""
{messagePrefix}
Pinging the server failed at {datetime.now()}
{messageSuffix}
"""
msg.attach(MIMEText(msgBody))
return msg
def send_email(self):
"""Creates a connection to the mailing server and sends email."""
context = ssl.create_default_context()
params = self.get_params()
with smtplib.SMTP(params.HOST, params.PORT) as server:
server.starttls(context=context)
server.login(params.USER, params.PASSWORD)
server.sendmail(params.FROM, params.TO, self.get_msg().as_string())
def get_params(self):
"""Getter for retrieving the email parameters."""
return self._Parms
def get_msg(self):
"""Getter for retrieving the message object."""
return self._msg
if __name__ == '__main__':
# Parse the arguments and send the email.
from args_parser import args_parser
args = args_parser()
SendEmailAlert(args.config_file, args.mail_from, args.mail_to, args.host,
args.port, args.user, args.password, args.subject,
args.msg_prefix, args.msg_suffix).send_email()