From 4308404cca36b309084a9a5b2e2b458c4934f77a Mon Sep 17 00:00:00 2001 From: Jack Levy Date: Sat, 14 Mar 2026 23:18:32 -0400 Subject: [PATCH] fix: use SMTP_SSL for port 465, STARTTLS for 587 Auto-detect SSL vs STARTTLS based on port number instead of always using SMTP + starttls(), which times out on port 465 (implicit SSL). Authored by: Jack Levy --- backend/app/api/notifications.py | 9 +++++++-- backend/app/workers/notification_dispatcher.py | 9 +++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/backend/app/api/notifications.py b/backend/app/api/notifications.py index 36ef944..4a82963 100644 --- a/backend/app/api/notifications.py +++ b/backend/app/api/notifications.py @@ -216,8 +216,13 @@ async def test_email( msg["From"] = from_addr msg["To"] = email_addr - with smtplib.SMTP(app_settings.SMTP_HOST, app_settings.SMTP_PORT, timeout=10) as s: - if app_settings.SMTP_STARTTLS: + use_ssl = app_settings.SMTP_PORT == 465 + if use_ssl: + ctx = smtplib.SMTP_SSL(app_settings.SMTP_HOST, app_settings.SMTP_PORT, timeout=10) + else: + ctx = smtplib.SMTP(app_settings.SMTP_HOST, app_settings.SMTP_PORT, timeout=10) + with ctx as s: + if not use_ssl and app_settings.SMTP_STARTTLS: s.starttls() if app_settings.SMTP_USER: s.login(app_settings.SMTP_USER, app_settings.SMTP_PASSWORD) diff --git a/backend/app/workers/notification_dispatcher.py b/backend/app/workers/notification_dispatcher.py index 5209dcd..de9d7dc 100644 --- a/backend/app/workers/notification_dispatcher.py +++ b/backend/app/workers/notification_dispatcher.py @@ -339,8 +339,13 @@ def _send_email( msg["List-Unsubscribe-Post"] = "List-Unsubscribe=One-Click" msg.attach(MIMEText(body, "plain", "utf-8")) - with smtplib.SMTP(app_settings.SMTP_HOST, app_settings.SMTP_PORT, timeout=10) as s: - if app_settings.SMTP_STARTTLS: + use_ssl = app_settings.SMTP_PORT == 465 + if use_ssl: + smtp_ctx = smtplib.SMTP_SSL(app_settings.SMTP_HOST, app_settings.SMTP_PORT, timeout=10) + else: + smtp_ctx = smtplib.SMTP(app_settings.SMTP_HOST, app_settings.SMTP_PORT, timeout=10) + with smtp_ctx as s: + if not use_ssl and app_settings.SMTP_STARTTLS: s.starttls() if app_settings.SMTP_USER: s.login(app_settings.SMTP_USER, app_settings.SMTP_PASSWORD)