/
send_mail.py
32 lines (27 loc) · 1.08 KB
/
send_mail.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
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import sys
fromaddr="ec2scriptnotification@gmail.com"
toaddr="sarithekanayake@gmail.com"
category=sys.argv[1]
respnose=sys.argv[2]
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
if category=="Response Code":
msg['Subject'] = "Web Server response error"
body = """Web Server script is getting {} code response""".format(respnose)
elif category=="Daily Backup" or category=="Web Server":
msg['Subject'] = "{} script Failed".format(category)
with open(respnose,'r') as file:
data = file.read()
body = """{} script is failed.\nFollowing are the outputs of {} file.\nErrors are on,\n\n{}\nof the script""".format(category,respnose,data)
msg.attach(MIMEText(body, 'plain'))
connection = smtplib.SMTP()
connection.connect('email-smtp.ap-southeast-1.amazonaws.com',587)
connection.starttls()
connection.login('AKIAZXI7XNIDH4XUNA56','BKJUpsGvX+Q0RSCoOEo46nWZSTHmoJ+BZ6N0zVykKqQ7')
text = msg.as_string()
connection.sendmail(fromaddr ,toaddr,text)
connection.close()