This repository has been archived by the owner on May 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 172
/
Copy pathsmtpnotification.py
56 lines (44 loc) · 1.63 KB
/
smtpnotification.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
__author__ = 'hoffmabc'
import smtplib
from smtplib import SMTPAuthenticationError
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from log import Logger
class SMTPNotification(object):
"""
A class for sending SMTP notifications
"""
def __init__(self, db):
self.db = db
self.log = Logger(system=self)
self.get_smtp_settings()
def get_smtp_settings(self):
settings = self.db.settings.get()
self.server = settings[15]
self.sender = settings[16]
self.recipient = settings[17]
self.username = settings[18]
self.password = settings[19]
def send(self, subject, body):
settings = self.db.settings.get()
is_enabled = True if settings[14] == 1 else False
if is_enabled:
# Construct MIME message
msg = MIMEMultipart('alternative')
msg['Subject'] = subject
msg['From'] = self.sender
msg['To'] = self.recipient
html_body = MIMEText(body, 'html')
msg.attach(html_body)
try:
server = smtplib.SMTP(self.server)
server.starttls()
# Authenticate if username/password set
if self.username and self.password:
server.login(self.username, self.password)
server.sendmail(self.sender, self.recipient, msg.as_string())
server.quit()
except SMTPAuthenticationError as e:
self.log.error('Authentication Error: %s' % e)
except Exception as e:
self.log.error(e)