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
+7 -5
View File
@@ -319,7 +319,8 @@ async def test_email():
the zone chosen for them. A blank password uses the saved one."""
from steward.core.formatting import zone
from steward.core.mail import Recipient, alert_email, is_real_address, pick_recipients
from steward.core.notifications import _send_email, smtp_mode
from steward.core import outbox
from steward.core.notifications import smtp_mode
from steward.models.users import User
form = await request.form
async with current_app.db_sessionmaker() as db:
@@ -345,11 +346,12 @@ async def test_email():
"email", False, "Your account has no email address mail can reach, and no Also send to "
"addresses are entered. Set your address on the Account page.")
alert = _sample_alert()
sent = await _send_email(cfg, alert, to)
# Queued and recorded like any email, tried once with the form's values.
ok, said = outbox.said(await outbox.send_each(
current_app, "test", to, lambda r: alert_email(cfg, alert, r), retry=False, cfg=cfg))
how = {"ssl": "implicit TLS", "starttls": "STARTTLS", "plain": "no TLS"}[smtp_mode(port, cfg["tls"])]
result = (f"Sent to {', '.join(r.address for r in to)} via {cfg['host']}:{port} ({how})."
if sent["sent"] else f"{cfg['host']}:{port} ({how}): {sent['error']}")
return await _notify_test_result("email", sent["sent"], result, alert_email(cfg, alert, to[0])[1])
result = f"{said[:-1]} via {cfg['host']}:{port} ({how})." if ok else f"{cfg['host']}:{port} ({how}): {said}"
return await _notify_test_result("email", ok, result, alert_email(cfg, alert, to[0])[1])
# ── Ansible Sources ───────────────────────────────────────────────────────────
+9 -6
View File
@@ -185,12 +185,15 @@ async def _deliver(invite: UserInvite, key: str) -> str | None:
url = invites.link(public_base_url(request), key)
subject, text, html = mail.invite_email(session.get("username", "An admin"), invite.role.value,
invite.expires_at, url, zone(session.get("tz")))
try:
await mail.send(cfg, [mail.message(cfg, invite.email, subject, text, html)])
except Exception as exc:
current_app.logger.warning("Invite email to %s failed: %s", invite.email, exc)
return f"Sending to {invite.email} failed ({exc})."
return None
from steward.core import outbox
(row,) = await outbox.send_each(current_app, "invite", [invite.email],
lambda _: (subject, text, html))
if row.state == "sent":
return None
if row.state == "pending":
return (f"Sending to {invite.email} didn't go through yet ({row.last_error}); "
f"Steward will keep trying for up to an hour.")
return f"Sending to {invite.email} failed ({row.last_error})."
async def _link_page(invite: UserInvite, key: str, why: str):