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
+11 -8
View File
@@ -49,7 +49,7 @@ def test_a_bad_template_raises_valueerror_saying_why(tpl, says):
n.render_webhook(tpl, ALERT)
# ── smtp_mode / _smtp_send_sync ───────────────────────────────────────────────
# ── smtp_mode / smtp_open (via outbox.deliver_sync) ───────────────────────────────────────────────
@pytest.mark.parametrize("port, tls, mode", [
(465, True, "ssl"), (465, False, "ssl"), ("465", False, "ssl"),
@@ -98,27 +98,30 @@ class SMTP_SSL(_FakeSMTP):
(25, False, [("connect", "SMTP", "mx", 25, n.SMTP_TIMEOUT_S)]),
])
def test_smtp_send_connects_by_mode_with_a_timeout(monkeypatch, port, tls, expected):
from email.message import EmailMessage
from steward.core import outbox
monkeypatch.setattr(n.smtplib, "SMTP", SMTP)
monkeypatch.setattr(n.smtplib, "SMTP_SSL", SMTP_SSL)
_FakeSMTP.made = []
msgs = [n.EmailMessage(), n.EmailMessage()]
msgs = [EmailMessage(), EmailMessage()]
msgs[0]["From"], msgs[1]["From"] = "steward@example.com", "other@example.com"
n._smtp_send_sync({"host": "mx", "port": port, "tls": tls, "username": "u", "password": "p"}, msgs)
cfg = {"host": "mx", "port": port, "tls": tls, "username": "u", "password": "p"}
assert outbox.deliver_sync(cfg, msgs) == [None, None]
(smtp,) = _FakeSMTP.made # every message over one connection
assert smtp.calls == expected + [("login", "u", "p"), ("send", "steward@example.com"),
("send", "other@example.com")]
async def test_email_from_falls_back_and_a_line_break_in_the_name_still_sends(monkeypatch):
def test_email_from_falls_back_and_a_line_break_in_the_name_still_sends():
from steward.core.mail import alert_email, message
sent = []
monkeypatch.setattr(n, "_smtp_send_sync",
lambda cfg, msgs: sent.extend((m["From"], m["Subject"]) for m in msgs))
to = [Recipient("a@example.com", timezone.utc, False)]
to = Recipient("a@example.com", timezone.utc, False)
base = {"host": "mx", "port": 587}
for cfg in ({**base, "from": "alerts@example.com", "username": "u@example.com"},
{**base, "from": "", "username": "u@example.com"},
{**base, "from": "", "username": ""}):
assert (await n._send_email(cfg, ALERT, to))["sent"]
m = message(cfg, to.address, *alert_email(cfg, ALERT, to))
sent.append((m["From"], m["Subject"]))
assert [f for f, _ in sent] == ["alerts@example.com", "u@example.com", "steward@localhost"]
# The rule name has a line break; the subject folds it to a space rather than failing.
assert sent[0][1] == '[Steward] Firing: Disk "root" on web01 \\ backslash — web01 94%'