PhotosAPI/modules/mailer.py

42 lines
1.4 KiB
Python
Raw Normal View History

2023-06-23 12:25:27 +03:00
import logging
2023-01-07 22:48:43 +02:00
from smtplib import SMTP, SMTP_SSL
from ssl import create_default_context
2023-06-22 14:17:53 +03:00
from traceback import print_exc
2023-06-23 12:25:27 +03:00
from modules.utils import configGet
logger = logging.getLogger(__name__)
2023-01-07 22:48:43 +02:00
try:
if configGet("use_ssl", "mailer", "smtp") is True:
mail_sender = SMTP_SSL(
configGet("host", "mailer", "smtp"),
configGet("port", "mailer", "smtp"),
)
2023-06-23 12:25:27 +03:00
logger.info("Initialized SMTP SSL connection")
2023-01-07 22:48:43 +02:00
elif configGet("use_tls", "mailer", "smtp") is True:
mail_sender = SMTP(
configGet("host", "mailer", "smtp"),
configGet("port", "mailer", "smtp"),
)
mail_sender.starttls(context=create_default_context())
mail_sender.ehlo()
2023-06-23 12:25:27 +03:00
logger.info("Initialized SMTP TLS connection")
2023-01-07 22:48:43 +02:00
else:
mail_sender = SMTP(
2023-03-12 15:59:13 +02:00
configGet("host", "mailer", "smtp"), configGet("port", "mailer", "smtp")
2023-01-07 22:48:43 +02:00
)
mail_sender.ehlo()
2023-06-23 12:25:27 +03:00
logger.info("Initialized SMTP connection")
2023-08-14 14:44:07 +03:00
except Exception as exc:
logger.error("Could not initialize SMTP connection to: %s", exc)
2023-01-07 22:48:43 +02:00
print_exc()
try:
mail_sender.login(
2023-03-12 15:59:13 +02:00
configGet("login", "mailer", "smtp"), configGet("password", "mailer", "smtp")
2023-01-07 22:48:43 +02:00
)
2023-06-23 12:25:27 +03:00
logger.info("Successfully initialized mailer")
2023-08-14 14:44:07 +03:00
except Exception as exc:
logger.error("Could not login into provided SMTP account due to: %s", exc)