forked from RinthLabs/PiKiln
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemail_notifications.py
More file actions
73 lines (56 loc) · 1.93 KB
/
email_notifications.py
File metadata and controls
73 lines (56 loc) · 1.93 KB
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
import requests
import os
import io
import datetime
import settings
# JSON for complete.html
# emailJSON = {
# "subject":"White Crackle Glaze 07 - Firing Complete!",
# "schedule":"White Crackle Glaze 07",
# "duration":"3:40",
# "cost":"$3.50"
# }
# JSON for self-check-error.html
# emailJSON = {
# "subject":"Self Check Error"
# }
# JSON for max-temp-error.hmtl
# emailJSON = {
# "subject":"Max Temperature Error"
# }
def GetTimestamp():
nowTime = datetime.datetime.now()
nowTimeFormat = nowTime.strftime("%Y-%m-%d %H:%M:%S")
print(nowTimeFormat)
return nowTimeFormat
def ReplaceParameter(baseString, parameter, content):
return baseString.replace("{{" + parameter + "}}", content)
def EmailContent(contentPath, contentJSON):
basePath = os.path.join('email', "email.html")
fullPath = os.path.join('email', contentPath)
# loads base email template
with open(basePath, 'r') as f:
baseEmailTemplate = f.read()
# loads content for email template
with open(fullPath, 'r') as f:
templateContent = f.read()
message = ReplaceParameter(baseEmailTemplate, "content", templateContent)
# Replaces all {{items}} in email with data from JSON
for key, value in contentJSON.items():
message = ReplaceParameter(message, key, value)
# print(message)
return message
# Sends email to receiver
def SendEmail(contentPath, contentJSON):
print("Send Email?")
if settings.settings['email'] != "":
print("Sending Email")
url = "https://p44f20evs1.execute-api.us-east-1.amazonaws.com/Production/send"
emailObject = {
"toEmail": settings.settings['email'],
"subject": contentJSON['subject'] + " " + GetTimestamp(),
"message": EmailContent(contentPath, contentJSON)
}
requests.post(url, json = emailObject)
print("Email Sent")
# return requests.post(url, json = myobj).text