Use logo1.png in emails, inline mail_style.css, attach summary as MD
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:24:08 +00:00
parent 917a7b8f2f
commit fb1dc3324d
5 changed files with 66 additions and 190 deletions
+39 -4
View File
@@ -7,6 +7,7 @@ Supports both plain text and HTML email bodies.
Template placeholders are primarily filled via environment variables.
"""
import base64
import os
import smtplib
import logging
@@ -53,6 +54,33 @@ def get_email_config():
}
def _load_css(path: str) -> str:
"""
Load CSS file content if it exists.
"""
if not path or not os.path.exists(path):
return ""
with open(path, "r", encoding="utf-8") as f:
return f.read()
def _logo1_inline_html() -> str:
"""
Return an inline <img> tag for logo1.png as base64.
Falls back to empty string if not found.
"""
logo_path = os.getenv("EMAIL_LOGO_PATH", "/app/src/misc/logo1.png")
if not os.path.exists(logo_path):
return ""
try:
with open(logo_path, "rb") as f:
b64 = base64.b64encode(f.read()).decode("utf-8")
return f'<img src="data:image/png;base64,{b64}" alt="A.P.Strom" style="max-width:180px; display:block; margin:0 auto 10px auto;"/>'
except Exception:
return ""
def build_template_context(**runtime_kwargs: Any) -> Dict[str, Any]:
"""
Build a context dict for templates from:
@@ -61,13 +89,20 @@ def build_template_context(**runtime_kwargs: Any) -> Dict[str, Any]:
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).
- EMAIL_CSS_PATH: path to mail_style.css (optional; we inline it)
- EMAIL_LOGO_PATH: path to logo1.png (default: /app/src/misc/logo1.png)
"""
# Load and inline mail_style.css for consistent email styling
css_path = os.getenv("EMAIL_CSS_PATH", "/app/src/misc/mail_style.css")
css_text = _load_css(css_path)
# Build inline logo HTML
logo_html = _logo1_inline_html()
ctx: Dict[str, Any] = {
"contact_email": os.getenv("EMAIL_CONTACT_ADDRESS", "support@example.com"),
"css_path": os.getenv("EMAIL_CSS_PATH", ""),
"email_css": css_text,
"email_logo": logo_html,
}
# Runtime values override env if provided