Refactor email configuration handling and improve logging for email delivery
Release Inventarsystem / release-docker (push) Successful in 2m25s

This commit is contained in:
2026-09-15 22:48:26 +02:00
parent 98b1e39abb
commit 92c86334e9
3 changed files with 33 additions and 12 deletions
+10 -2
View File
@@ -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: