feat(email): the report, invites, resets and test sends go through the outbox; only it talks SMTP (#5508)
CI / lint (push) Successful in 2s
CI / integration (push) Failing after 1m11s
CI / unit (push) Successful in 1m26s
CI / publish (push) Skipped

#5508 "Every other email through the outbox: weekly report, invites,
password resets, and the test buttons" (milestone 501).

- outbox.send_each queues one email per reader (claimed, so the sender
  doesn't take it too) and tries them at once, for a page that answers
  with what happened; outbox.said words the result. attempt takes a cfg
  for a test of the form's unsaved settings and returns the settled rows.
- Weekly report (scheduled and Send now) and invites: queued, tried at
  once; one the server couldn't take yet is retried for an hour. An invite
  that didn't go shows its link with the reason, as before.
- Password reset: queued in the reset link's own transaction; the sender
  delivers it on its next tick.
- Settings -> Notifications test email and a rule's Send test: queued and
  recorded with retry=False, tried once (System 156 ruling).
- Removed mail.send, notifications._send_email and _smtp_send_sync;
  dispatch_notifications is webhook only. A guard asserts only the outbox
  (and smtp_open itself) opens an SMTP connection, with a self-test.

Also fixes run 8941 (#5507's integration test): its Also-send-to
addresses were "@test", which is_real_address rightly drops; they are
example.com now.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-09 16:05:01 -04:00
co-authored by Claude Opus 5.5
parent 5237ffc663
commit 9a3e7ddba2
19 changed files with 277 additions and 148 deletions
+5 -32
View File
@@ -4,7 +4,6 @@ import logging
import smtplib
import ssl
import types
from email.message import EmailMessage
import httpx
from jinja2.sandbox import SandboxedEnvironment
@@ -12,20 +11,11 @@ from jinja2.sandbox import SandboxedEnvironment
logger = logging.getLogger(__name__)
async def dispatch_notifications(
smtp_cfg: dict,
webhook_cfg: dict,
alert_data: dict,
recipients: list = (),
) -> dict:
"""Dispatch all configured notification channels: email to each of
`recipients` (mail.Recipient, from mail.recipients) when a server is set.
Returns results dict."""
async def dispatch_notifications(webhook_cfg: dict, alert_data: dict) -> dict:
"""Post an alert's webhook when one is set; {"webhook": result} or {}.
Email goes through the outbox instead (core.outbox, #5506)."""
results = {}
if smtp_cfg.get("host") and recipients:
results["email"] = await _send_email(smtp_cfg, alert_data, recipients)
if webhook_cfg.get("url"):
results["webhook"] = await _send_webhook(webhook_cfg, alert_data)
@@ -43,21 +33,10 @@ def smtp_mode(port, tls) -> str:
return "starttls" if tls else "plain"
async def _send_email(cfg: dict, alert: dict, recipients: list) -> dict:
"""Each recipient their own copy, in their own zone, over one connection."""
from steward.core.mail import alert_email, message, send
try:
messages = [message(cfg, to.address, *alert_email(cfg, alert, to)) for to in recipients]
await send(cfg, messages)
return {"sent": True, "error": None}
except Exception as exc:
logger.error(f"Email notification failed: {exc}")
return {"sent": False, "error": str(exc)}
def smtp_open(cfg: dict) -> smtplib.SMTP:
"""A connection to the mail server, TLS and login done, every step
bounded by SMTP_TIMEOUT_S. The caller closes it (use it in `with`)."""
bounded by SMTP_TIMEOUT_S. The caller closes it (use it in `with`).
Only core.outbox.deliver_sync opens one: every email is a queued row."""
port = int(cfg.get("port") or 587)
mode = smtp_mode(port, cfg.get("tls", True))
ctx = ssl.create_default_context()
@@ -76,12 +55,6 @@ def smtp_open(cfg: dict) -> smtplib.SMTP:
return smtp
def _smtp_send_sync(cfg: dict, messages: list[EmailMessage]) -> None:
with smtp_open(cfg) as smtp:
for msg in messages:
smtp.send_message(msg)
def _json_str(value) -> str:
"""A value as it may sit between the quotes of a JSON string."""
return json.dumps("" if value is None else str(value))[1:-1]