mirror of
https://github.com/rommapp/romm.git
synced 2026-11-28 17:48:31 +01:00
- A channel's min_level is its own enum (info, warning, error), so the API refuses `success` and neither the list nor the dialog maps it. - One home each for the admin-reaches-the-LAN rule (the DB handler now returns the owner's role) and for describing a failed delivery. - One webhook config builder for create and update, which drops the secret of a Discord webhook on both. - SMTP_SECURITY outside starttls/tls/none leaves email off, so the heartbeat says so, with a warning at startup. - The attempts counter gets its TTL in the same round trip as INCR. - Deliveries group channels by user and serialize a notification only when a channel wants it. - The v2 side reuses errorMessage (now with a fallback) instead of its own detail parsing, mirrors the backend length limits as constants, shares one channel test factory, and derives `created` on save from the list instead of carrying it on the event. - Drop RBadge's inline override, which nothing needs now that an inline badge gets no anchor class, and trim docstrings to one sentence. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
76 lines
2.5 KiB
Python
76 lines
2.5 KiB
Python
"""Email deliveries over the SMTP server the SMTP_* settings describe."""
|
|
|
|
import smtplib
|
|
import socket
|
|
import ssl
|
|
from email.message import EmailMessage
|
|
from typing import Final
|
|
|
|
import config
|
|
|
|
TIMEOUT_SECONDS: Final = 30
|
|
|
|
|
|
class EmailError(RuntimeError):
|
|
"""Email isn't set up, or the SMTP server refused the message."""
|
|
|
|
|
|
def _one_line(text: str) -> str:
|
|
# A header can't carry a line break, and a title from the API may hold one.
|
|
return " ".join(text.split())
|
|
|
|
|
|
def build_message(to: str, subject: str, text: str) -> EmailMessage:
|
|
message = EmailMessage()
|
|
message["From"] = config.SMTP_FROM
|
|
message["To"] = to
|
|
message["Subject"] = _one_line(subject)
|
|
message.set_content(text)
|
|
return message
|
|
|
|
|
|
def send_email(to: str, subject: str, text: str) -> None:
|
|
"""Send a plain-text email, blocking until the server takes it.
|
|
|
|
Raises:
|
|
EmailError: Email isn't set up, or the server refused the message.
|
|
"""
|
|
if not config.EMAIL_ENABLED:
|
|
raise EmailError("Email isn't set up on this server")
|
|
# Refused rather than read as `none`, which would log in over plaintext.
|
|
if config.SMTP_SECURITY not in config.SMTP_SECURITY_MODES:
|
|
raise EmailError(
|
|
f"SMTP_SECURITY is {config.SMTP_SECURITY!r}, not one of "
|
|
+ ", ".join(config.SMTP_SECURITY_MODES)
|
|
)
|
|
|
|
message = build_message(to, subject, text)
|
|
context = ssl.create_default_context()
|
|
# smtplib otherwise greets with socket.getfqdn(), a DNS lookup that can
|
|
# stall for seconds, and in a container it names the same host anyway.
|
|
local_hostname = socket.gethostname()
|
|
try:
|
|
if config.SMTP_SECURITY == "tls":
|
|
smtp: smtplib.SMTP = smtplib.SMTP_SSL(
|
|
config.SMTP_HOST,
|
|
config.SMTP_PORT,
|
|
local_hostname=local_hostname,
|
|
timeout=TIMEOUT_SECONDS,
|
|
context=context,
|
|
)
|
|
else:
|
|
smtp = smtplib.SMTP(
|
|
config.SMTP_HOST,
|
|
config.SMTP_PORT,
|
|
local_hostname=local_hostname,
|
|
timeout=TIMEOUT_SECONDS,
|
|
)
|
|
with smtp:
|
|
if config.SMTP_SECURITY == "starttls":
|
|
smtp.starttls(context=context)
|
|
if config.SMTP_USERNAME:
|
|
smtp.login(config.SMTP_USERNAME, config.SMTP_PASSWORD)
|
|
smtp.send_message(message)
|
|
except (smtplib.SMTPException, OSError) as exc:
|
|
raise EmailError(f"The SMTP server refused the message: {exc}") from exc
|