Add SMTP security mode selector with SSL/STARTTLS/none options and automatic port switching
Replaced boolean `smtp_use_tls` with explicit `smtp_security` enum field supporting "none", "starttls", and "ssl" modes. Added database migration to create `smtp_security` column with "starttls" default. Updated mailer to use SMTP_SSL client for direct SSL connections on port 465 and SMTP with STARTTLS for port 587. Modified admin settings UI to show dropdown selector with encryption options and auto-adjust
This commit is contained in:
@@ -23,9 +23,13 @@ def send_html_email(settings: AppSettings, recipient: str, subject: str, body_ht
|
||||
message.add_alternative(body_html, subtype="html")
|
||||
|
||||
context = ssl.create_default_context()
|
||||
with smtplib.SMTP(settings.smtp_host, settings.smtp_port or 587, timeout=20) as server:
|
||||
security = getattr(settings, "smtp_security", "") or ("starttls" if settings.smtp_use_tls else "none")
|
||||
port = settings.smtp_port or (465 if security == "ssl" else 587)
|
||||
smtp_client = smtplib.SMTP_SSL if security == "ssl" else smtplib.SMTP
|
||||
connection = smtp_client(settings.smtp_host, port, timeout=20, context=context) if security == "ssl" else smtp_client(settings.smtp_host, port, timeout=20)
|
||||
with connection as server:
|
||||
server.ehlo()
|
||||
if settings.smtp_use_tls:
|
||||
if security == "starttls":
|
||||
server.starttls(context=context)
|
||||
server.ehlo()
|
||||
if settings.smtp_username:
|
||||
|
||||
Reference in New Issue
Block a user