forked from ledudu/smartCheckin
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Notify.py
52 lines (44 loc) · 1.32 KB
/
Notify.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
#!/usr/bin/env python
#-*- coding: utf-8 -*-
# import datetime
import smtplib
import email.mime.text
import sys
from time import sleep
class Notify_Admin:
"""docstring for Notify_Admin"""
target = ""
username = ''
password = ''
host = "smtp.gmail.com"
port = 587
def __init__(self, target, username, password):
self.target = target
self.username = username
self.password = password
def check_error(self, filename):
lines = file(filename, 'r').readlines()
Errors = ""
for each in lines:
if each.find("failed") != -1:
Errors += each
if Errors != "":
self.send_mail(Errors)
def send_mail(self, text):
print "Connecting Gmail Server ..."
smtp = smtplib.SMTP(self.host, self.port, timeout=25)
smtp.starttls()
try:
print 'Logging Gmail Server ...'
smtp.login(self.username, self.password)
except:
print "Login Error *****"
sys.exit()
msg = email.mime.text.MIMEText(text)
msg['From'] = self.username
msg['To'] = self.target
msg['Subject'] = "SmartCheckin Error !"
print msg.as_string()
smtp.sendmail(self.username, self.target, msg.as_string())
sleep(5)
smtp.quit()