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
|
#!/usr/bin/env python
import random
import logging
import smtplib
class JobFrequency:
MINUTE = "minute"
HOUR = "hour"
DAY = "day"
DAY_NOON = "day_noon"
class JobBase:
def __init__(self):
self.config = None
def getName(self):
return str(self.__class__)
def shouldExecute(self, cronmode):
frequency = self.executeEvery()
if cronmode == frequency:
return True
return False
def setConfig(self, config):
self.config = config
def sendEmail(self, subject, body, to=""):
return sendEmail(self.config, subject, body, to)
def executeEvery(self):
pass
def execute(self):
pass
def sendEmail(config, subject, body, to=""):
FROM = config.get('email', 'user')
PASS = config.get('email', 'pass')
if not to:
to = config.get('general', 'alertcontact')
# Prepare actual message
# Avoid gmail threading
subject = "[" + config.get('general', 'servername') + "] " + subject + " " + str(random.random())
message = """\From: %s\nTo: %s\nSubject: %s\n\n%s""" \
% (FROM, ", ".join(to), subject, body)
try:
server = smtplib.SMTP(config.get('email', 'smtpserver'), config.get('email', 'smtpport'))
server.ehlo()
server.starttls()
server.login(FROM, PASS)
server.sendmail(FROM, to, message)
server.close()
return True
except Exception as e:
logging.critical("Caught an exception trying to send an email:" + str(e))
return False
|