fix(messaging): test email + EmailLog show effective config, not stale env
Some checks failed
CI / ruff (push) Successful in 17s
CI / validate (push) Has been cancelled
CI / dependency-scanning (push) Has been cancelled
CI / docs (push) Has been cancelled
CI / deploy (push) Has been cancelled
CI / pytest (push) Has been cancelled

Two display bugs where admin-facing email diagnostics reported the
.env-level config even when database overrides were in effect — so a
prod admin who switched from SendGrid (.env) to SMTP via /admin/settings
would receive a test email whose body still said "Provider: sendgrid"
and "From: noreply@wizard.lu", even though the actual From header used
the DB-overridden address. Confusing, and erodes trust in the page that
exists specifically to verify the setup.

- admin_settings.send_test_email: read get_effective_email_config(db)
  and display its provider + from_email in the test email's body. Also
  use the effective provider when stamping the audit-log row.
- email_service: in the "template not found" EmailLog branch, record
  the effective from_email / from_name / provider from
  self._platform_config (already populated correctly by __init__),
  instead of falling back to the env-only settings.email_*.

Verified by importing both modules; pure plumbing fix, no schema or
behavior change for callers.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-10 21:30:41 +02:00
parent b65c51c038
commit f2d1bdcd49
2 changed files with 16 additions and 8 deletions

View File

@@ -656,10 +656,16 @@ def send_test_email(
"""
Send a test email using the platform email configuration.
This tests the email provider configuration from environment variables.
Reports the *effective* config — DB overrides if any, else .env. The
test email's body must match what actually got used to send it,
otherwise admins can't trust the page they're testing from.
"""
from app.modules.messaging.services.email_service import EmailService
effective = get_effective_email_config(db)
eff_provider = effective.get("provider", app_settings.email_provider)
eff_from = effective.get("from_email", app_settings.email_from_address)
try:
email_service = EmailService(db)
@@ -676,13 +682,13 @@ def send_test_email(
<p>If you received this email, your email settings are working correctly!</p>
<hr style="border: none; border-top: 1px solid #e5e7eb; margin: 20px 0;">
<p style="color: #6b7280; font-size: 12px;">
Provider: {app_settings.email_provider}<br>
From: {app_settings.email_from_address}
Provider: {eff_provider}<br>
From: {eff_from}
</p>
</body>
</html>
""",
body_text=f"Test email from Orion platform.\n\nProvider: {app_settings.email_provider}\nFrom: {app_settings.email_from_address}",
body_text=f"Test email from Orion platform.\n\nProvider: {eff_provider}\nFrom: {eff_from}",
is_platform_email=True,
)
@@ -695,7 +701,7 @@ def send_test_email(
action="send_test_email",
target_type="email",
target_id=request.to_email,
details={"provider": app_settings.email_provider},
details={"provider": eff_provider},
)
db.commit()

View File

@@ -1317,16 +1317,18 @@ class EmailService:
if not resolved:
logger.error(f"Email template not found: {template_code} ({resolved_language})")
# Create failed log entry
# Use the *effective* config so the audit row reflects what the
# admin actually configured (DB overrides win over .env).
log = EmailLog(
template_code=template_code,
recipient_email=to_email,
recipient_name=to_name,
subject=f"[Template not found: {template_code}]",
from_email=settings.email_from_address,
from_name=settings.email_from_name,
from_email=self._platform_config.get("from_email", settings.email_from_address),
from_name=self._platform_config.get("from_name", settings.email_from_name),
status=EmailStatus.FAILED.value,
error_message=f"Template not found: {template_code} ({resolved_language})",
provider=settings.email_provider,
provider=self._platform_config.get("provider", settings.email_provider),
store_id=store_id,
user_id=user_id,
related_type=related_type,