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
+6 -5
View File
@@ -253,11 +253,12 @@ jobs:
INVENTAR_SECRET_KEY: ${{secrets.INVENTAR_SECRET_KEY}} INVENTAR_SECRET_KEY: ${{secrets.INVENTAR_SECRET_KEY}}
INVENTAR_DATA_ENCRYPTION_KEY: ${{secrets.INVENTAR_DATA_ENCRYPTION_KEY}} INVENTAR_DATA_ENCRYPTION_KEY: ${{secrets.INVENTAR_DATA_ENCRYPTION_KEY}}
INVENTAR_MONGODB_PASSWORD: ${{secrets.INVENTAR_MONGODB_PASSWORD}} INVENTAR_MONGODB_PASSWORD: ${{secrets.INVENTAR_MONGODB_PASSWORD}}
EMAIL_ENABLED: ${{secrets.EMAIL_ENABLED}} EMAIL_ENABLED: "${{ secrets.EMAIL_ENABLED }}"
EMAIL_SMTP_HOST: ${{secrets.EMAIL_SMTP_HOST}} EMAIL_SMTP_HOST: "${{ secrets.EMAIL_SMTP_HOST }}"
EMAIL_SMTP_PORT: ${{secrets.EMAIL_SMTP_PORT}} EMAIL_SMTP_PORT: "${{ secrets.EMAIL_SMTP_PORT }}"
EMAIL_USERNAME: ${{secrets.EMAIL_USERNAME}} EMAIL_USERNAME: "${{ secrets.EMAIL_USERNAME }}"
EMAIL_PASSWORD: ${{secrets.EMAIL_PASSWORD}} EMAIL_PASSWORD: "${{ secrets.EMAIL_PASSWORD }}"
EMAIL_FROM_ADDRESS: "${{ secrets.EMAIL_FROM_ADDRESS }}"
expose: expose:
- "8000" - "8000"
volumes: volumes:
+17 -5
View File
@@ -220,13 +220,25 @@ SSL_CERT = _get(_conf, ['ssl', 'cert'], DEFAULTS['ssl']['cert'])
SSL_KEY = _get(_conf, ['ssl', 'key'], DEFAULTS['ssl']['key']) SSL_KEY = _get(_conf, ['ssl', 'key'], DEFAULTS['ssl']['key'])
# Email settings # Email settings
EMAIL_ENABLED = bool(os.getenv('EMAIL_ENABLED', False)) def _get_email_env(name, default=''):
EMAIL_SMTP_HOST = str(os.getenv('EMAIL_SMTP_HOST', False)) value = os.getenv(name)
if value is None:
return default
value = value.strip()
return '' if value.lower() in {'', 'false', 'none', 'null'} else value
EMAIL_ENABLED = _get_email_env('EMAIL_ENABLED').lower() in {'1', 'true', 'yes', 'on'}
EMAIL_SMTP_HOST = _get_email_env('EMAIL_SMTP_HOST')
EMAIL_SMTP_PORT = int(os.getenv('EMAIL_SMTP_PORT', 587)) EMAIL_SMTP_PORT = int(os.getenv('EMAIL_SMTP_PORT', 587))
EMAIL_USE_TLS = True EMAIL_USE_TLS = True
EMAIL_USERNAME = str(os.getenv('EMAIL_USERNAME', False)) EMAIL_USERNAME = _get_email_env('EMAIL_USERNAME')
EMAIL_PASSWORD = str(os.getenv('EMAIL_PASSWORD', False)) EMAIL_PASSWORD = _get_email_env('EMAIL_PASSWORD')
EMAIL_FROM_ADDRESS = _get(_conf, ['email', 'from_address'], EMAIL_USERNAME) EMAIL_FROM_ADDRESS = (
_get_email_env('EMAIL_FROM_ADDRESS')
or _get(_conf, ['email', 'from_address'], '')
or EMAIL_USERNAME
)
EMAIL_DEFAULT_SENDER_NAME = "Invario Inventarsystem Sender" EMAIL_DEFAULT_SENDER_NAME = "Invario Inventarsystem Sender"
EMAIL_TIMEOUT_SECONDS = 20 EMAIL_TIMEOUT_SECONDS = 20
+10 -2
View File
@@ -3,11 +3,18 @@ from email.mime.text import MIMEText
from email.mime.application import MIMEApplication from email.mime.application import MIMEApplication
import smtplib import smtplib
import time import time
import logging
import Web.modules.database.settings as cfg import Web.modules.database.settings as cfg
logger = logging.getLogger(__name__)
def _build_smtp_client(): def _build_smtp_client():
if not cfg.EMAIL_SMTP_HOST:
raise RuntimeError('EMAIL_SMTP_HOST ist nicht konfiguriert')
smtp = smtplib.SMTP( smtp = smtplib.SMTP(
cfg.EMAIL_SMTP_HOST, cfg.EMAIL_SMTP_HOST,
cfg.EMAIL_SMTP_PORT, 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: 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.""" """Send a plain/HTML message, optionally with one PDF attachment."""
if not cfg.MODULES.is_enabled("mail"): 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 return False
recipients = _normalize_recipients(email) recipients = _normalize_recipients(email)
if not recipients: if not recipients:
logger.warning("Email delivery skipped because no recipients were provided")
return False return False
body_message = note body_message = note
@@ -104,7 +112,7 @@ def _send_message(email: list | str, subject: str, note: str, sender: str, attac
return True return True
except Exception as e: except Exception as e:
print(f"Debug: Fehler beim Senden der E-Mail: {e}") logger.exception("Email delivery failed: %s", e)
return False return False
finally: finally:
if smtp: if smtp: