add ip change notification and refine sender display
This commit is contained in:
+46
-3
@@ -1,7 +1,9 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from datetime import UTC, datetime
|
||||
from email.message import EmailMessage
|
||||
from email.utils import formataddr
|
||||
import smtplib
|
||||
|
||||
from app.config import Settings
|
||||
@@ -21,6 +23,7 @@ class SMTPConfig:
|
||||
port: int
|
||||
username: str
|
||||
password: str
|
||||
from_name: str
|
||||
from_address: str
|
||||
to_address: str
|
||||
use_starttls: bool
|
||||
@@ -47,6 +50,7 @@ def get_smtp_config(settings: Settings, *, require_enabled: bool = True) -> SMTP
|
||||
port=settings.smtp_port,
|
||||
username=settings.smtp_username,
|
||||
password=settings.smtp_password,
|
||||
from_name=settings.smtp_from_name,
|
||||
from_address=settings.smtp_from_address,
|
||||
to_address=settings.smtp_to_address,
|
||||
use_starttls=settings.smtp_use_starttls,
|
||||
@@ -72,7 +76,7 @@ def send_plaintext_email(
|
||||
smtp_config = get_smtp_config(settings, require_enabled=require_enabled)
|
||||
message = EmailMessage()
|
||||
message["Subject"] = subject
|
||||
message["From"] = smtp_config.from_address
|
||||
message["From"] = _build_from_header(smtp_config)
|
||||
message["To"] = recipient or smtp_config.to_address
|
||||
message.set_content(body)
|
||||
|
||||
@@ -84,7 +88,11 @@ def send_plaintext_email(
|
||||
smtp.ehlo()
|
||||
if smtp_config.username:
|
||||
smtp.login(smtp_config.username, smtp_config.password)
|
||||
smtp.send_message(message)
|
||||
smtp.send_message(
|
||||
message,
|
||||
from_addr=smtp_config.from_address,
|
||||
to_addrs=[recipient or smtp_config.to_address],
|
||||
)
|
||||
except (OSError, smtplib.SMTPException) as exc:
|
||||
error_message = _sanitize_error_message(str(exc), smtp_config.password)
|
||||
raise EmailDeliveryError(error_message or "SMTP delivery failed") from exc
|
||||
@@ -99,8 +107,43 @@ def send_smtp_test_email(settings: Settings) -> None:
|
||||
)
|
||||
|
||||
|
||||
def send_public_ip_changed_email(
|
||||
settings: Settings,
|
||||
*,
|
||||
previous_ipv4: str,
|
||||
current_ipv4: str,
|
||||
detected_at: datetime,
|
||||
) -> None:
|
||||
send_plaintext_email(
|
||||
settings,
|
||||
subject="Public IP changed",
|
||||
body=(
|
||||
"Your public IPv4 address has changed.\n\n"
|
||||
f"Previous IP: {previous_ipv4}\n"
|
||||
f"Current IP: {current_ipv4}\n"
|
||||
f"Detected at: {_format_utc_timestamp(detected_at)}\n\n"
|
||||
"If you use Namecheap API trusted IP restrictions, you may need to "
|
||||
"update the trusted IP manually.\n"
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def _sanitize_error_message(message: str, password: str) -> str:
|
||||
sanitized = message
|
||||
if password:
|
||||
sanitized = sanitized.replace(password, "[redacted]")
|
||||
return sanitized
|
||||
return sanitized
|
||||
|
||||
|
||||
def _format_utc_timestamp(value: datetime) -> str:
|
||||
if value.tzinfo is None:
|
||||
normalized = value.replace(tzinfo=UTC)
|
||||
else:
|
||||
normalized = value.astimezone(UTC)
|
||||
return normalized.strftime("%Y-%m-%d %H:%M:%S UTC")
|
||||
|
||||
|
||||
def _build_from_header(smtp_config: SMTPConfig) -> str:
|
||||
if smtp_config.from_name:
|
||||
return formataddr((smtp_config.from_name, smtp_config.from_address))
|
||||
return smtp_config.from_address
|
||||
Reference in New Issue
Block a user