Refactor email configuration handling and improve logging for email delivery
Release Inventarsystem / release-docker (push) Successful in 2m25s
Release Inventarsystem / release-docker (push) Successful in 2m25s
This commit is contained in:
@@ -3,11 +3,18 @@ from email.mime.text import MIMEText
|
||||
from email.mime.application import MIMEApplication
|
||||
import smtplib
|
||||
import time
|
||||
import logging
|
||||
|
||||
import Web.modules.database.settings as cfg
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _build_smtp_client():
|
||||
if not cfg.EMAIL_SMTP_HOST:
|
||||
raise RuntimeError('EMAIL_SMTP_HOST ist nicht konfiguriert')
|
||||
|
||||
smtp = smtplib.SMTP(
|
||||
cfg.EMAIL_SMTP_HOST,
|
||||
cfg.EMAIL_SMTP_PORT,
|
||||
@@ -31,11 +38,12 @@ def _normalize_recipients(email: list | str) -> list[str]:
|
||||
def _send_message(email: list | str, subject: str, note: str, sender: str, attachment=None) -> bool:
|
||||
"""Send a plain/HTML message, optionally with one PDF attachment."""
|
||||
if not cfg.MODULES.is_enabled("mail"):
|
||||
print("Debug: Module not enabled")
|
||||
logger.info("Email delivery skipped because the mail module is disabled")
|
||||
return False
|
||||
|
||||
recipients = _normalize_recipients(email)
|
||||
if not recipients:
|
||||
logger.warning("Email delivery skipped because no recipients were provided")
|
||||
return False
|
||||
|
||||
body_message = note
|
||||
@@ -104,7 +112,7 @@ def _send_message(email: list | str, subject: str, note: str, sender: str, attac
|
||||
|
||||
return True
|
||||
except Exception as e:
|
||||
print(f"Debug: Fehler beim Senden der E-Mail: {e}")
|
||||
logger.exception("Email delivery failed: %s", e)
|
||||
return False
|
||||
finally:
|
||||
if smtp:
|
||||
|
||||
Reference in New Issue
Block a user