-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailer.py
84 lines (64 loc) · 2.28 KB
/
mailer.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""Send status messages via email"""
import signal
import smtplib
__author__ = "Matt Martini"
__email__ = "[email protected]"
__version__ = "0.1.0"
class Mailer:
"""Send email (and SMS via email) on alert condition."""
def __init__(self, subject, message):
"""Initialize streamer."""
self.subject = subject
self.message = message
self.header = ""
self.silence = False
self.sender = "[email protected]"
self.receivers = [
]
def send(self):
"""Send prepaired message via smtp"""
self.header = """From: Greenhouse Controller <[email protected]>
To: <[email protected]>
"""
self.header = self.header + "Subject: " + self.subject + "\n\n"
self.message = self.header + self.message
if not self.silence:
try:
smtp_obj = smtplib.SMTP("cathal.local")
smtp_obj.sendmail(self.sender, self.receivers, self.message)
print("Successfully sent email")
self.stall()
except smtplib.SMTPException:
print("Error: unable to send email")
def stall_handler(self, *args):
"""Reset silence flag afer alarm goes off."""
self.silence = False
def stall(self):
"""Wait 10 minutes before sending another email."""
self.silence = True
signal.signal(signal.SIGALRM, self.stall_handler)
signal.alarm(600)
def cleanup(self):
"""Cleanup alarm."""
signal.alarm(0)
self.silence = False
def __del__(self):
"""Ensure cleanup."""
self.cleanup()
def test():
"""Test config methods."""
subject = "Alert Test"
message = "You can ignore this test message. But you should only get one."
msg = Mailer(subject, message)
print("DEBUG: Sending message, it should send.")
print(msg.message)
msg.send()
msg.subject = "Alert Silence Test"
msg.message = "You should not receive this message. It should be silenced."
print("DEBUG: Attempting second message during silence period, should not send.")
msg.send()
print("DEBUG: This line should print even as the second email was not sent.")
msg.cleanup()
if __name__ == "__main__":
test()