-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathsmtp_functions.py
54 lines (40 loc) · 1.83 KB
/
smtp_functions.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
import datetime
import logging
import smtplib
import typing
from email.message import EmailMessage
from dynaconf import Dynaconf
def create_connection_and_log_user(config: Dynaconf) -> typing.Union[smtplib.SMTP_SSL, smtplib.SMTP]:
"""Create connection and log user to the SMTP server using the configuration in config."""
if "smtp_port" in config.notification:
port = config.notification.smtp_port
else:
port = 0
if "use_ssl" in config.notification and config.notification.use_ssl:
host = smtplib.SMTP_SSL(config.notification.smtp_server, port)
else:
host = smtplib.SMTP(config.notification.smtp_server, port)
if "use_tls" in config.notification and config.notification.use_tls:
host.starttls()
if config.notification.smtp_username and config.notification.smtp_password:
host.login(config.notification.smtp_username, config.notification.smtp_password)
return host
def send_email(error: str, config: Dynaconf) -> None:
"""Sends email with provided error using the settings in config."""
msg = EmailMessage()
msg["Subject"] = config.notification.email_subject
msg["From"] = config.notification.email_sender
msg["To"] = ", ".join(config.notification.email_recipients)
date_time = datetime.datetime.now().strftime("%d/%m/%Y %H:%M:%S")
error = f"{date_time}: {error}"
msg.set_content(error)
sender_email = config.notification.email_sender
if "smtp_username" in config.notification:
sender_email = config.notification.smtp_username
try:
smtp_conn = create_connection_and_log_user(config)
smtp_conn.sendmail(sender_email, config.notification.email_recipients, msg.as_string())
smtp_conn.quit()
logging.debug("Notification email sent.")
except:
logging.debug("Failed to send notification email!")