feat(email): each account gets its own email, in its own zone (milestone 495)
CI / lint (push) Successful in 3s
CI / integration (push) Successful in 1m0s
CI / unit (push) Successful in 1m24s
CI / publish (push) Successful in 1m32s

Milestone 495 "Email goes to each account, in its own time zone", on the
operator's ruling (System 156): notifications go to each user, times in
their zone, defaults on, toggled per user or per role.

- users.notify_alerts / notify_report (migration 0036): None follows the
  role's default, notify.<kind>.<role> in settings, all on to start.
- mail.pick_recipients: active accounts that want the email and have an
  address mail can reach (an SSO placeholder like name@oidc is skipped and
  flagged), each in its own zone with no UTC beside it, then the Also send
  to addresses in their chosen zone with UTC beside. One copy per address,
  all over one SMTP connection.
- Alerts, the weekly report and the test email (to you, in your zone, plus
  the typed addresses) send that way. The save-time smtp.tz capture is gone.
- Notifications: a role × email table of defaults, who each email reaches
  and who can't be reached, and Also send to with its own zone, which starts
  as the first admin's to open the page.
- Account page: your address and your two switches. Users: an Emailed
  column, and an admin sets an account's switches on its page.
- The Alerts page counts the accounts it emails. zone_select macro and
  formatting.zones_by_region shared by Account and Notifications.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-08 15:16:17 -04:00
co-authored by Claude Opus 5.5
parent c533eec33a
commit 22090b02d8
27 changed files with 601 additions and 132 deletions
+58 -13
View File
@@ -1,6 +1,6 @@
{# steward/templates/settings/notifications.html #}
{% extends "base.html" %}
{% from "_macros.html" import crumbs %}
{% from "_macros.html" import crumbs, zone_select %}
{% block title %}Settings — Notifications — Steward{% endblock %}
{% from "settings/_tabs.html" import settings_tabs %}
{% block section_tabs %}{{ settings_tabs("notifications") }}{% endblock %}
@@ -51,23 +51,68 @@
placeholder="steward@example.com">
</div>
<div class="form-group">
<label>Recipients <span style="color:var(--text-muted);font-size:0.8rem;">(comma-separated)</span></label>
<input type="text" name="smtp.recipients"
value="{{ settings['smtp.recipients'] | join(', ') }}"
placeholder="admin@example.com, ops@example.com">
<p style="font-size:0.78rem;color:var(--text-muted);margin-top:0.25rem;">
Alert emails give times in {{ tz_label() }}, your zone when you save, with UTC beside them.
{% if not settings['general.public_base_url'] %}Set Steward's address in <a href="/settings/">General</a> and they link back here.{% endif %}
</p>
</div>
<button type="button" class="btn btn-ghost" hx-post="/settings/notifications/test-email"
hx-include="closest form" hx-target="#email-test-result" hx-swap="innerHTML">
Send a test email</button>
<p class="field-hint">It goes to you, at your account's address and in your zone, and to the Also send to addresses below.</p>
<div id="email-test-result"></div>
</div>
{# ── Who gets email (milestone 495) ───────────────────────────────────────── #}
<section class="card" aria-labelledby="notify-who">
<h2 id="notify-who" class="section-title" style="margin-bottom:0.75rem;">Who gets email</h2>
<p class="field-hint" style="margin-bottom:0.75rem;">
Each account gets its own copy at its own address, with times in its own zone. These are the defaults
for each role; an account's own choice, on its Account page or in <a href="/settings/users/">Users</a>, wins.</p>
<table class="table">
<thead><tr><th>Role</th><th>Alert emails</th><th>Weekly report</th></tr></thead>
<tbody>
{% for r in roles %}
<tr>
<td>{{ r.value|capitalize }}</td>
{% for kind in ("alerts", "report") %}
{% set key = "notify." ~ kind ~ "." ~ r.value %}
<td><label class="field-check"><input type="checkbox" name="{{ key }}" {% if settings[key] %}checked{% endif %}
aria-label="{{ 'Alert emails' if kind == 'alerts' else 'Weekly report' }} for {{ r.value }}s"><span>On</span></label></td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
<p class="page-verdict" style="margin-top:0.75rem;">
Alerts reach {{ audience.alerts|length }} account{{ 's' if audience.alerts|length != 1 }}
{%- if audience.alerts %} ({{ audience.alerts|map(attribute='username')|join(', ') }}){% endif %}
· the weekly report reaches {{ audience.report|length }}
{%- if audience.report %} ({{ audience.report|map(attribute='username')|join(', ') }}){% endif %}.
</p>
{% if audience.unreachable %}
<p class="field-hint tone-warn">
{{ audience.unreachable|length }} account{{ 's have' if audience.unreachable|length != 1 else ' has' }} no address mail can reach:
{% for u in audience.unreachable %}<a href="/settings/users/{{ u.id }}">{{ u.username }}</a>{{ ", " if not loop.last }}{% endfor %}.
</p>
{% endif %}
</section>
{# ── Also send to ─────────────────────────────────────────────────────────── #}
<section class="card" aria-labelledby="notify-extra">
<h2 id="notify-extra" class="section-title" style="margin-bottom:0.75rem;">Also send to</h2>
<div class="form-group">
<label for="smtp-recipients">Addresses <span style="color:var(--text-muted);font-size:0.8rem;">(comma-separated)</span></label>
<input type="text" id="smtp-recipients" name="smtp.recipients"
value="{{ settings['smtp.recipients'] | join(', ') }}"
placeholder="ops@example.com, pager@example.com">
<p class="field-hint">For a mailbox that isn't a person. These get alerts and the weekly report.</p>
</div>
<div class="form-group">
<label for="smtp-tz">Their times are in</label>
{{ zone_select("smtp.tz", "smtp-tz", settings['smtp.tz'] or "UTC", zones) }}
<p class="field-hint">With UTC beside, since nobody's own zone is known for them.</p>
</div>
{% if not settings['general.public_base_url'] %}
<p class="field-hint">Set Steward's address in <a href="/settings/general/">General</a> and every email links back here.</p>
{% endif %}
</section>
{# ── Webhook ───────────────────────────────────────────────────────────── #}
<div class="card">
<h2 class="section-title" style="margin-bottom:1.25rem;">Webhook</h2>
@@ -94,7 +139,7 @@
<code>{{ '{{ alert.rule_name }}' }}</code> is escaped to sit inside a JSON string, so quotes or
line breaks in a name can't break the body; a missing value reads "no data".
For a bare JSON value use <code>{{ '{{ raw.value | tojson }}' }}</code> (a number, or null).
Fields: state, rule_name, resource, metric, value, threshold, source_module, note, timestamp.</p>
Fields: state, rule_name, resource, metric, value, operator, threshold, source_module, note, timestamp.</p>
</div>
<button type="button" class="btn btn-ghost" hx-post="/settings/notifications/test-webhook"
+2 -1
View File
@@ -54,7 +54,8 @@
</div>
<p style="font-size:0.78rem;color:var(--text-muted);margin-top:0.25rem;">
Requires SMTP configured in <a href="/settings/notifications/">Notifications</a>.
It goes to each account that has the weekly report on, in that account's own zone, and to the
Also send to addresses; who that is, and the mail server, are in <a href="/settings/notifications/">Notifications</a>.
{% if last_sent %}Last sent {{ last_sent|when }}.{% endif %}
</p>
+4
View File
@@ -5,6 +5,7 @@
{% extends "base.html" %}
{% from "_macros.html" import crumbs, page_header, status_badge %}
{% from "settings/_tabs.html" import settings_tabs %}
{% from "account/_email_choices.html" import email_choices with context %}
{% block title %}{{ user.username }} — Users — Steward{% endblock %}
{% block section_tabs %}{{ settings_tabs("users") }}{% endblock %}
{% block breadcrumb %}{{ crumbs([("Settings", "/settings/"), ("Users", "/settings/users/"), (user.username, "")]) }}{% endblock %}
@@ -41,6 +42,9 @@
</section>
</form>
{% if not reachable %}<p class="field-hint tone-warn">{{ user.email }} isn't an address mail can reach, so this account gets no email. Set a real one above.</p>{% endif %}
{{ email_choices(user, defaults, "/settings/users/" ~ user.id ~ "/email", with_address=false) }}
<section class="card form-section" aria-labelledby="u-role">
<h2 id="u-role" class="section-title">Role</h2>
{% if not local %}
+3 -1
View File
@@ -17,7 +17,7 @@
<div class="card-flush">
<table class="table">
<thead><tr><th>User</th><th>Signs in with</th><th>Role</th><th>Last sign-in</th><th class="num">AI tokens</th></tr></thead>
<thead><tr><th>User</th><th>Signs in with</th><th>Role</th><th>Emailed</th><th>Last sign-in</th><th class="num">AI tokens</th></tr></thead>
<tbody>
{% for u in users %}
<tr class="row-link{% if not u.is_active %} row-off{% endif %}">
@@ -28,6 +28,8 @@
</td>
<td>{{ sources.get(u.auth_source, u.auth_source) }}</td>
<td>{{ u.role.value|capitalize }}{% if not u.is_active %} {{ status_badge("inactive", "Disabled") }}{% endif %}</td>
<td>{% if u.id not in reachable %}<span class="tone-warn" title="{{ u.email }} isn't an address mail can reach">no address</span>
{%- elif gets[u.id] %}{{ gets[u.id]|join(", ") }}{% else %}—{% endif %}</td>
<td class="nowrap">{% if last.get(u.id) %}{{ last[u.id]|ago }}{% else %}—{% endif %}</td>
<td class="num">{{ tokens.get(u.id, 0) or "—" }}</td>
</tr>