diff --git a/.github/workflows/release-docker.yml b/.github/workflows/release-docker.yml index 08b8ef1..765abb9 100644 --- a/.github/workflows/release-docker.yml +++ b/.github/workflows/release-docker.yml @@ -253,11 +253,12 @@ jobs: INVENTAR_SECRET_KEY: ${{secrets.INVENTAR_SECRET_KEY}} INVENTAR_DATA_ENCRYPTION_KEY: ${{secrets.INVENTAR_DATA_ENCRYPTION_KEY}} INVENTAR_MONGODB_PASSWORD: ${{secrets.INVENTAR_MONGODB_PASSWORD}} - EMAIL_ENABLED: ${{secrets.EMAIL_ENABLED}} - EMAIL_SMTP_HOST: ${{secrets.EMAIL_SMTP_HOST}} - EMAIL_SMTP_PORT: ${{secrets.EMAIL_SMTP_PORT}} - EMAIL_USERNAME: ${{secrets.EMAIL_USERNAME}} - EMAIL_PASSWORD: ${{secrets.EMAIL_PASSWORD}} + EMAIL_ENABLED: "${{ secrets.EMAIL_ENABLED }}" + EMAIL_SMTP_HOST: "${{ secrets.EMAIL_SMTP_HOST }}" + EMAIL_SMTP_PORT: "${{ secrets.EMAIL_SMTP_PORT }}" + EMAIL_USERNAME: "${{ secrets.EMAIL_USERNAME }}" + EMAIL_PASSWORD: "${{ secrets.EMAIL_PASSWORD }}" + EMAIL_FROM_ADDRESS: "${{ secrets.EMAIL_FROM_ADDRESS }}" expose: - "8000" volumes: diff --git a/Web/modules/database/settings.py b/Web/modules/database/settings.py index 9221d19..d45cff7 100644 --- a/Web/modules/database/settings.py +++ b/Web/modules/database/settings.py @@ -220,13 +220,25 @@ SSL_CERT = _get(_conf, ['ssl', 'cert'], DEFAULTS['ssl']['cert']) SSL_KEY = _get(_conf, ['ssl', 'key'], DEFAULTS['ssl']['key']) # Email settings -EMAIL_ENABLED = bool(os.getenv('EMAIL_ENABLED', False)) -EMAIL_SMTP_HOST = str(os.getenv('EMAIL_SMTP_HOST', False)) +def _get_email_env(name, default=''): + 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_USE_TLS = True -EMAIL_USERNAME = str(os.getenv('EMAIL_USERNAME', False)) -EMAIL_PASSWORD = str(os.getenv('EMAIL_PASSWORD', False)) -EMAIL_FROM_ADDRESS = _get(_conf, ['email', 'from_address'], EMAIL_USERNAME) +EMAIL_USERNAME = _get_email_env('EMAIL_USERNAME') +EMAIL_PASSWORD = _get_email_env('EMAIL_PASSWORD') +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_TIMEOUT_SECONDS = 20 diff --git a/Web/modules/emailservice/email.py b/Web/modules/emailservice/email.py index 42f57ef..0d7fa6f 100644 --- a/Web/modules/emailservice/email.py +++ b/Web/modules/emailservice/email.py @@ -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: