Files
romm/backend/handler/email_handler.py
zurdi 5059d2625e feat: email password reset links when SMTP is set up
A reset link only ever went to the server log, for an admin to pass on. It
now goes to the user's email address when SMTP is set up, the user has one
and ROMM_BASE_URL is shareable, falling back to the log otherwise or when the
server refuses the message. The link is built from ROMM_BASE_URL alone, never
the request's Host, so a forged header can't aim it at another server; it
goes out after the response, so its timing doesn't reveal whether the
username exists; and a user's inbox gets at most one a minute.

The SMTP sender moves from the notification channels to
handler/email_handler.py now that it has a second caller. The v2 reset form
says the link is on its way when the server sends email.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-23 17:36:47 +02:00

70 lines
2.2 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")
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