-
Notifications
You must be signed in to change notification settings - Fork 0
/
mail.py
80 lines (64 loc) · 2.23 KB
/
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
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
import smtplib
from email.message import EmailMessage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from datetime import datetime
from phonebook import phonebook
#define now
now = datetime.now().strftime("%I:%M%p on %B %d, %Y")
#create list of emails
to_emails=[]
for name, [number, email] in phonebook.items():
if email:
to_emails.append(email)
print(to_emails)
# set up server
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login('', '')
#send log to ultracold@gmail.com every two weeks
def update():
msg_update = MIMEMultipart()
msg_update['To'] = ''
msg_update['From'] = ''
msg_update['Subject'] = ''
body = "Attached is log of power check program as of %s." % now
content = MIMEText(body, 'plain')
msg_update.attach(content)
#attach log
with open('power.log', 'r') as f:
log = MIMEText(f.read())
log.add_header('Content-Disposition', 'log', filename='power.log')
msg_update.attach(log)
server.send_message(msg_update)
#send exception to ultracold@gmail.com as soon as it occurs
def exception(exception):
msg_exception = MIMEMultipart()
msg_exception['To'] = ''
msg_exception['From'] = ''
msg_exception['Subject'] = 'Power check software exception'
body = "Exception logged at %s: %s" % (now, exception)
content = MIMEText(body, 'plain')
msg_exception.attach(content)
server.send_message(msg_exception)
#send outage alert to emails in phonebook as soon as it occur
def outage():
msg_outage = MIMEMultipart()
msg_outage['To'] =', '.join(to_emails)
msg_outage['From'] = ''
msg_outage['Subject'] = 'URGENT: Power outage alert'
body = "Power outage recorded at %s." % now
content = MIMEText(body, 'plain')
msg_outage.attach(content)
server.send_message(msg_outage)
#send power restored alert to emails in phonebook as soon as it occurs
def restored():
msg_restored = MIMEMultipart()
msg_restored['To'] = ', '.join(to_emails)
msg_restored['From'] = ''
msg_restored['Subject'] = 'Power restored'
body = "Power restored at %s" % now
content = MIMEText(body, 'plain')
msg_restored.attach(content)
server.send_message(msg_restored)