This commit is contained in:
retoor 2025-11-11 15:30:31 +01:00
parent d957968e6f
commit f2735b19e7

View File

@ -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}")