From f2735b19e7cc396b20bac665fed701005eb3a5cd Mon Sep 17 00:00:00 2001 From: retoor Date: Tue, 11 Nov 2025 15:30:31 +0100 Subject: [PATCH] Update.: --- rbox/mail.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/rbox/mail.py b/rbox/mail.py index 56edb48..ba6aac3 100644 --- a/rbox/mail.py +++ b/rbox/mail.py @@ -79,7 +79,8 @@ class EmailService: msg.attach(html_part) try: - if settings.SMTP_PORT == 465: + # Use implicit TLS for port 465 or when SMTP_USE_TLS is True + if settings.SMTP_PORT == 465 or settings.SMTP_USE_TLS: async with aiosmtplib.SMTP( hostname=settings.SMTP_HOST, port=settings.SMTP_PORT, @@ -90,11 +91,19 @@ class EmailService: await smtp.send_message(msg) print(f"Email sent to {task.to_email}") else: + # Use STARTTLS for other ports async with aiosmtplib.SMTP( hostname=settings.SMTP_HOST, port=settings.SMTP_PORT, ) as smtp: - await smtp.starttls() + try: + await smtp.starttls() + except Exception as tls_error: + if "already using" in str(tls_error).lower() or "tls" in str(tls_error).lower(): + # Connection is already using TLS, proceed without starttls + pass + else: + raise await smtp.login(settings.SMTP_USERNAME, settings.SMTP_PASSWORD) await smtp.send_message(msg) print(f"Email sent to {task.to_email}")