Compare commits

..
1 Commits
Author SHA1 Message Date
Andrew Beveridge ☄️andClaude Opus 5.5 5181e42b46 fix(email): failed SMTP QUIT after an accepted send doesn't fail the send (v0.263.2) (#1113)
## Summary
This is a follow-up to #1112. CodeRabbit found the issue while reviewing
the same fix in karaoke-decide. If Postmark accepts the message but the
SMTP `QUIT` fails during the `with` block's exit, `_send_via_smtp`
reported a failure, and a caller retry could then send the email twice.
Cleanup errors are now swallowed, falling back to `close()`, and only
errors from the send itself fail the send.

## Testing
- Added `test_quit_failure_after_send_still_reports_success`. Email
tests: 72 passed.

The same change was reviewed by local CodeRabbit in karaoke-decide#147.

@coderabbitai ignore

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-02 14:38:42 -04:00
3 changed files with 22 additions and 3 deletions

No files matched your search

+9 -1
View File
@@ -397,10 +397,18 @@ class PostmarkEmailProvider(EmailProvider):
else:
msg.set_content(html_content, subtype="html")
with smtplib.SMTP(self.SMTP_HOST, self.SMTP_PORT, timeout=self.SMTP_TIMEOUT) as smtp:
smtp = smtplib.SMTP(self.SMTP_HOST, self.SMTP_PORT, timeout=self.SMTP_TIMEOUT)
try:
smtp.starttls(context=ssl.create_default_context())
smtp.login(self.server_token, self.server_token)
smtp.send_message(msg, to_addrs=[to_email, *cc, *bcc])
finally:
# A failed QUIT after an accepted send must not fail the send
# (a caller retry would then duplicate the email).
try:
smtp.quit()
except (smtplib.SMTPException, OSError):
smtp.close()
except (smtplib.SMTPException, OSError, ValueError):
# ValueError: EmailMessage rejects CR/LF in header values.
logger.exception(f"Failed to send email to {to_email} via Postmark SMTP fallback")
+12 -1
View File
@@ -288,7 +288,7 @@ class TestPostmarkRetry:
@pytest.fixture
def mock_smtp(self):
with patch("backend.services.email_service.smtplib.SMTP") as smtp_cls:
self.smtp = smtp_cls.return_value.__enter__.return_value
self.smtp = smtp_cls.return_value
yield smtp_cls
def test_html_403_falls_back_to_smtp_without_api_retries(self, provider, mock_smtp):
@@ -362,6 +362,17 @@ class TestPostmarkRetry:
assert result.success is False
self.smtp.send_message.assert_not_called()
def test_quit_failure_after_send_still_reports_success(self, provider, mock_smtp):
"""Postmark already accepted the message; a failed QUIT must not trigger a resend."""
import smtplib as _smtplib
self.smtp.quit.side_effect = _smtplib.SMTPServerDisconnected("gone")
with patch("backend.services.email_service.requests.post") as mock_post:
mock_post.return_value = self._response(403, json_body=False)
result = self._send(provider)
assert result.success is True
self.smtp.close.assert_called_once()
def test_smtp_success_clears_stale_message_id(self, provider, mock_smtp):
provider.last_message_id = "old"
with patch("backend.services.email_service.requests.post") as mock_post:
+1 -1
View File
@@ -1,6 +1,6 @@
[tool.poetry]
name = "karaoke-gen"
version = "0.263.1"
version = "0.263.2"
description = "Generate karaoke videos with synchronized lyrics. Handles the entire process from downloading audio and lyrics to creating the final video with title screens."
authors = ["Andrew Beveridge <andrew@beveridge.uk>"]
license = "MIT"