-
Notifications
You must be signed in to change notification settings - Fork 0
/
email_utils.py
56 lines (34 loc) · 1.38 KB
/
email_utils.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
# Standard
import smtplib
# External
import nikocraft as nc
# Local
from configs import MainConfig
ERROR_EMAIL = """
! ATTENTION !
A critical error occurred on Makerspace Arcade! The system should \
restart automatically. Please take a look at the system!
-----------------------------------------
TIME
%TIME%
-----------------------------------------
ERROR MESSAGE
%ERROR%
-----------------------------------------
* This email was automatically generated by Makerspace Arcade.
"""
def send(server: smtplib.SMTP, sender: str, target: str, subject: str, content: str) -> None:
data = f"From:{sender}\nTo:{target}\nSubject:{subject}\n\n{content}"
server.sendmail(sender, target, data)
def send_error(config: MainConfig, error_message: str) -> None:
try:
print("Send emails ...")
with smtplib.SMTP_SSL(config.email_server_address, port=config.email_server_port) as server:
server.login(config.email_server_user, config.email_server_password)
content = ERROR_EMAIL.replace("%TIME%", nc.time.datetime_f_dmy_hms()).replace("%ERROR%", error_message)
for target in config.email_targets:
send(server, config.email_sender, target, "CRITICAL ERROR - Msp Arcade", content)
print("Emails sent successfully.")
except Exception as e:
print("Sending emails failed!")
print(f"{type(e).__name__}: {e}")