Skip to content

fix(email): use SMTP_SSL for port 465 and fall back to IPv4 on timeout - #46061

Closed
liuhao1024 wants to merge 1 commit into
NousResearch:mainfrom
liuhao1024:fix/email-smtp-ipv6
Closed

fix(email): use SMTP_SSL for port 465 and fall back to IPv4 on timeout#46061
liuhao1024 wants to merge 1 commit into
NousResearch:mainfrom
liuhao1024:fix/email-smtp-ipv6

Conversation

@liuhao1024

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes the Email gateway adapter to use SMTP_SSL (implicit TLS) for port 465 connections and adds IPv4 fallback when IPv6 is unreachable. Previously, the adapter always used SMTP() + starttls(), which is correct for port 587 but causes hangs/failures on port 465 providers.

Related Issue

Fixes #46018

Type of Change

  • 🐛 Bug fix (non-breaking change that fixes an issue)

Changes Made

  • gateway/platforms/email.py: Extracted _connect_smtp() helper that selects SMTP_SSL for port 465 and SMTP + STARTTLS for other ports. Added IPv4 fallback when the default connection times out (handles hosts with AAAA records on networks without IPv6 routing). Replaced 4 duplicate SMTP connection sites with calls to the helper.
  • tests/gateway/test_email.py: Added TestConnectSmtp class with 4 tests: port 587 uses SMTP+STARTTLS, port 465 uses SMTP_SSL, IPv6 timeout falls back to IPv4 for port 587, and IPv6 timeout falls back to IPv4 for port 465.

How to Test

  1. Configure Email gateway with a port 465 provider (e.g., EMAIL_SMTP_PORT=465)
  2. Start the gateway — SMTP connection should succeed using SMTP_SSL
  3. Configure with a host that has AAAA records but no IPv6 routing — connection should fall back to IPv4 within the timeout
  4. Run pytest tests/gateway/test_email.py -x -q — all 66 tests pass

Checklist

Code

  • I've read the Contributing Guide
  • My commit messages follow Conventional Commits (fix(scope):, feat(scope):, etc.)
  • I searched for existing PRs to make sure this isn't a duplicate
  • My PR contains only changes related to this fix/feature (no unrelated commits)
  • I've run pytest tests/gateway/test_email.py -q and all tests pass
  • I've added tests for my changes (required for bug fixes, strongly encouraged for features)
  • I've tested on my platform: macOS

Documentation & Housekeeping

  • I've updated relevant documentation (README, docs/, docstrings) — or N/A
  • I've updated cli-config.yaml.example if I added/changed config keys — or N/A
  • I've updated CONTRIBUTING.md or AGENTS.md if I changed architecture or workflows — or N/A
  • I've considered cross-platform impact (Windows, macOS) per the compatibility guide — or N/A
  • I've updated tool descriptions/schemas if I changed tool behavior — or N/A

Code Intelligence

  • Analyzed: gateway/platforms/email.py (_connect_smtp, _send_email, _send_email_with_attachments, _send_email_with_attachment, connect)
  • Blast radius: LOW — changes are confined to SMTP connection establishment; send logic and message formatting are unchanged
  • Related patterns: IMAP already uses imaplib.IMAP4_SSL (implicit TLS) — this fix brings SMTP to parity

Port 465 expects implicit TLS (SMTP_SSL) from the first byte. The email
adapter always used SMTP() + starttls(), which is correct for port 587
but hangs/fails on port 465 providers (e.g., Swiss ISPs).

Additionally, when the SMTP host has AAAA DNS records but IPv6 is
unreachable, socket.create_connection() tries IPv6 first and hangs
until timeout. Add an IPv4 fallback via AF_INET socket.

Extract _connect_smtp() helper to consolidate the 4 duplicate SMTP
connection sites into a single method with correct protocol selection
and IPv6 fallback logic.
@teknium1

Copy link
Copy Markdown
Contributor

Merged via PR #46084: #46084

Your SMTP_SSL port-465 fix was cherry-picked onto current main with authorship preserved. We added a follow-up to make the IPv4 fallback use supported smtplib extension points, avoid global resolver monkeypatching, and preserve TLS verification failure semantics.

Thanks for the fix.

@teknium1 teknium1 closed this Jun 14, 2026
@alt-glitch alt-glitch added type/bug Something isn't working comp/gateway Gateway runner, session dispatch, delivery platform/email Email (IMAP/SMTP) adapter P2 Medium — degraded but workaround exists duplicate This issue or pull request already exists labels Jun 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/gateway Gateway runner, session dispatch, delivery duplicate This issue or pull request already exists P2 Medium — degraded but workaround exists platform/email Email (IMAP/SMTP) adapter type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: Email adapter fails on port 465 (SMTP_SSL) and hangs on IPv6

3 participants