Compare commits

...

1 Commits

Author SHA1 Message Date
Aiirondev_dev 92c86334e9 Refactor email configuration handling and improve logging for email delivery
Release Inventarsystem / release-docker (push) Successful in 2m25s
2026-09-15 22:48:26 +02:00
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_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:
+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'])
# 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
+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: