feat(email): each account gets its own email, in its own zone (milestone 495)
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:
@@ -1,5 +1,4 @@
|
||||
from __future__ import annotations
|
||||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
import smtplib
|
||||
@@ -17,12 +16,15 @@ async def dispatch_notifications(
|
||||
smtp_cfg: dict,
|
||||
webhook_cfg: dict,
|
||||
alert_data: dict,
|
||||
recipients: list = (),
|
||||
) -> dict:
|
||||
"""Dispatch all configured notification channels. Returns results dict."""
|
||||
"""Dispatch all configured notification channels: email to each of
|
||||
`recipients` (mail.Recipient, from mail.recipients) when a server is set.
|
||||
Returns results dict."""
|
||||
results = {}
|
||||
|
||||
if smtp_cfg.get("host") and smtp_cfg.get("recipients"):
|
||||
results["email"] = await _send_email(smtp_cfg, alert_data)
|
||||
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)
|
||||
@@ -41,20 +43,19 @@ def smtp_mode(port, tls) -> str:
|
||||
return "starttls" if tls else "plain"
|
||||
|
||||
|
||||
async def _send_email(cfg: dict, alert: dict) -> dict:
|
||||
from steward.core.mail import alert_email, message
|
||||
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:
|
||||
subject, text, html = alert_email(cfg, alert)
|
||||
msg = message(cfg, subject, text, html)
|
||||
loop = asyncio.get_running_loop()
|
||||
await loop.run_in_executor(None, _smtp_send_sync, cfg, msg)
|
||||
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_send_sync(cfg: dict, msg: EmailMessage) -> None:
|
||||
def _smtp_send_sync(cfg: dict, messages: list[EmailMessage]) -> None:
|
||||
port = int(cfg.get("port") or 587)
|
||||
mode = smtp_mode(port, cfg.get("tls", True))
|
||||
ctx = ssl.create_default_context()
|
||||
@@ -67,7 +68,8 @@ def _smtp_send_sync(cfg: dict, msg: EmailMessage) -> None:
|
||||
smtp.starttls(context=ctx)
|
||||
if cfg.get("username"):
|
||||
smtp.login(cfg["username"], cfg.get("password", ""))
|
||||
smtp.send_message(msg)
|
||||
for msg in messages:
|
||||
smtp.send_message(msg)
|
||||
|
||||
|
||||
def _json_str(value) -> str:
|
||||
|
||||
Reference in New Issue
Block a user