Make email template placeholders configurable via environment variables
Mirror and run GitLab CI / build (push) Has been cancelled
Ruff / ruff (push) Has been cancelled

This commit is contained in:
admin
2026-06-14 15:11:53 +00:00
parent 85cdd9216a
commit 917a7b8f2f
3 changed files with 46 additions and 15 deletions
+31 -3
View File
@@ -4,6 +4,7 @@ Email sender module for ScrAIbe.
Sends transcription outputs (TXT, JSON, etc.) via SMTP.
All credentials are configured via environment variables.
Supports both plain text and HTML email bodies.
Template placeholders are primarily filled via environment variables.
"""
import os
@@ -13,7 +14,7 @@ from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from typing import List, Optional
from typing import List, Optional, Dict, Any
logger = logging.getLogger("scraibe.email_sender")
@@ -52,7 +53,31 @@ def get_email_config():
}
def load_template(template_name: str, **kwargs) -> str:
def build_template_context(**runtime_kwargs: Any) -> Dict[str, Any]:
"""
Build a context dict for templates from:
- environment variables (base, customizable)
- runtime-provided values (override env if present)
Environment variables:
- EMAIL_CONTACT_ADDRESS: value for {contact_email}
- EMAIL_CSS_PATH: value for {css_path}
Runtime kwargs are merged on top (e.g. queue_position, exception).
"""
ctx: Dict[str, Any] = {
"contact_email": os.getenv("EMAIL_CONTACT_ADDRESS", "support@example.com"),
"css_path": os.getenv("EMAIL_CSS_PATH", ""),
}
# Runtime values override env if provided
if runtime_kwargs:
ctx.update(runtime_kwargs)
return ctx
def load_template(template_name: str, **runtime_kwargs: Any) -> str:
"""
Load an HTML email template from misc/ and render placeholders.
@@ -70,9 +95,12 @@ def load_template(template_name: str, **kwargs) -> str:
with open(path, "r", encoding="utf-8") as f:
template = f.read()
# Build context from env + runtime
ctx = build_template_context(**runtime_kwargs)
# Replace {placeholder} style variables safely
try:
return template.format(**kwargs)
return template.format(**ctx)
except KeyError as e:
raise EmailError(f"Missing template variable: {e}")