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
This commit is contained in:
@@ -216,8 +216,13 @@ async def test_email(
|
|||||||
msg["From"] = from_addr
|
msg["From"] = from_addr
|
||||||
msg["To"] = email_addr
|
msg["To"] = email_addr
|
||||||
|
|
||||||
with smtplib.SMTP(app_settings.SMTP_HOST, app_settings.SMTP_PORT, timeout=10) as s:
|
use_ssl = app_settings.SMTP_PORT == 465
|
||||||
if app_settings.SMTP_STARTTLS:
|
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()
|
s.starttls()
|
||||||
if app_settings.SMTP_USER:
|
if app_settings.SMTP_USER:
|
||||||
s.login(app_settings.SMTP_USER, app_settings.SMTP_PASSWORD)
|
s.login(app_settings.SMTP_USER, app_settings.SMTP_PASSWORD)
|
||||||
|
|||||||
@@ -339,8 +339,13 @@ def _send_email(
|
|||||||
msg["List-Unsubscribe-Post"] = "List-Unsubscribe=One-Click"
|
msg["List-Unsubscribe-Post"] = "List-Unsubscribe=One-Click"
|
||||||
msg.attach(MIMEText(body, "plain", "utf-8"))
|
msg.attach(MIMEText(body, "plain", "utf-8"))
|
||||||
|
|
||||||
with smtplib.SMTP(app_settings.SMTP_HOST, app_settings.SMTP_PORT, timeout=10) as s:
|
use_ssl = app_settings.SMTP_PORT == 465
|
||||||
if app_settings.SMTP_STARTTLS:
|
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()
|
s.starttls()
|
||||||
if app_settings.SMTP_USER:
|
if app_settings.SMTP_USER:
|
||||||
s.login(app_settings.SMTP_USER, app_settings.SMTP_PASSWORD)
|
s.login(app_settings.SMTP_USER, app_settings.SMTP_PASSWORD)
|
||||||
|
|||||||
Reference in New Issue
Block a user