-
Notifications
You must be signed in to change notification settings - Fork 1
/
send_mail.py
46 lines (43 loc) · 1.34 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# import gmail
import smtplib
import json
from email.mime.text import MIMEText
import os
import requests
import traceback
import time
def slack_notification(message):
headers = {
"Content-Type": "application/json"
}
data = json.dumps({
"text": "In KWOC Website following error occured :\n{}".format(message)
})
r = requests.post(
os.environ["SLACK_WEBHOOK_URL"], headers=headers, data=data)
if r.status_code != 200:
print("in slack_notification : {}".format(r.status_code))
print(r.text)
def send_mail(mail_subject, mail_body, to_email):
# credentials = json.load(open('CONFIG', 'r'))
msg = MIMEText(mail_body)
msg.set_type("text/html")
msg['Subject'] = mail_subject
# print (msg)
# sending mail
flag =0
while True :
try:
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(os.environ["EMAIL"],os.environ["PASSWORD"])
server.sendmail(os.environ["EMAIL"], to_email, msg.as_string())
server.quit()
return True
except :
flag+=1
if flag >= 5 :
slack_notification("Got following error while sending email : \n{}".format(traceback.format_exc()))
return False
time.sleep(1)
# return False