-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotify.py
60 lines (50 loc) · 1.33 KB
/
notify.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
#!/usr/bin/env python3
import json
import emails
import jinja2
html_tpl = """
<html>
<head>
</head>
<body>
<ul>
{% for error in msg.splitlines() %}
<li>{{error}}</li>
{% endfor %}
</ul>
</body>
</html>
""".lstrip()
tpl = jinja2.Template(html_tpl)
def send_report(topic):
try:
return _send_report(topic)
except:
return {"success": False, "status": 0}
def _send_report(topic):
with open('/media/data/email.json') as fd:
settings = json.load(fd)
with open('/etc/hostname') as fd:
hostname = fd.read().strip()
html = tpl.render(msg=topic)
smtp = {
'host': settings['email_smtp_server'],
'port': settings['email_smtp_port'], 'timeout': 5,
'tls': settings['email_smtp_ssl'],
'user': settings['email_login'],
'password': settings['email_password'],
}
message = emails.Message(
html=html,
subject='Fehlermeldung',
mail_from=(hostname, settings['email_from'])
)
r = message.send(to=[settings['email_to']], smtp=smtp)
if r.status_code == 250:
# print('Success')
return {"success": True, "status": 250}
else:
# print('Error:', r.status_code)
return {"success": False, "status": r.status_code}
if __name__ == '__main__':
send_report('Testbenachrichtigung')