fix(smtp): prevent double STARTTLS causing "Connection already using TLS" error#20
Merged
cofin merged 1 commit intoMay 3, 2026
Conversation
…mtplib When use_tls=True and use_ssl=False, aiosmtplib's auto-STARTTLS (start_tls=None default) would upgrade the connection, then litestar-email's manual starttls() call would fail with 'Connection already using TLS'. Pass start_tls=False to disable aiosmtplib's auto-negotiation, letting litestar-email manage STARTTLS explicitly. Add regression tests for STARTTLS, implicit SSL, and plain scenarios.
Contributor
Author
|
@cofin PTAL, thanks! |
Welcome to Codecov 🎉Once you merge this PR into your default branch, you're all set! Codecov will compare coverage reports and display results in all future pull requests. Thanks for integrating Codecov - We've got you covered ☂️ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fix SMTP STARTTLS double execution that causes
EmailConnectionError: SMTP connection error: Connection already using TLSwhen usingSMTPConfig(use_tls=True, use_ssl=False)with any STARTTLS-capable server (port 587).Closes #19
Problem
When connecting to an SMTP server that supports STARTTLS (e.g.,
smtp.office365.com:587), the TLS upgrade is executed twice:aiosmtplib.SMTPwithout passingstart_tls(defaults toNone)start_tls=None→ auto-detect → server supports STARTTLS → auto-upgrades ✅use_tls=True→ manually callsstarttls()again → 💥Connection already using TLSThis affects all SMTP servers requiring STARTTLS (Office 365, Gmail, AWS SES, etc.), making the documented
use_tls=True+use_ssl=Falseconfiguration completely broken.Solution
Explicitly pass
start_tls=Falsetoaiosmtplib.SMTP()to disable its auto-STARTTLS negotiation, letting litestar-email manage the STARTTLS upgrade manually as intended:This is the minimal, non-breaking fix (Option A from the issue). The existing manual
starttls()call remains the single point of TLS upgrade.Changes
src/litestar_email/backends/smtp.py— Addstart_tls=Falseparameter toaiosmtplib.SMTP()constructorsrc/tests/test_smtp_backend.py— Add 3 regression tests:test_smtp_backend_starttls_no_double_call— Verifiesstart_tls=Falseis passed and manualstarttls()is called exactly oncetest_smtp_backend_ssl_does_not_call_starttls— Verifies implicit SSL (use_ssl=True) does not trigger manual STARTTLStest_smtp_backend_no_tls_no_ssl— Verifies plain connection does not call STARTTLSChecklist
make testpasses (129 tests)make lintpasses (ruff, mypy, pyright, slotscheck)