fix(email): use implicit TLS for port 465 - #46084
Conversation
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.
🔎 Lint report:
|
|
Verification: clean review — email adapter implicit TLS + IPv4 fallback Reviewed the full diff. Two changes in this PR, both well-implemented: 1. Port 465 implicit TLS (SMTP_SSL)
2. IPv4 fallback for unreachable IPv6
Resource management: No findings. Tests cover all 4 connection variants (587/465 × default/IPv4-fallback) plus the TLS-error-doesn't-retry guard. |
Summary
Email gateway SMTP connections now use the correct TLS mode for port 465 and a thread-safe IPv4 fallback for broken IPv6 routes.
Cherry-picks #46061 from @liuhao1024, then fixes the fallback path so it uses supported
smtplibextension points instead of unsupportedsock=kwargs. Also adds security-focused regressions: certificate/TLS verification errors are not retried through fallback, and the IPv4 path does not monkeypatch process-global socket resolution.Changes
gateway/platforms/email.py: add_connect_smtp()withSMTP_SSLfor port 465 andSMTP+STARTTLSelsewhere.gateway/platforms/email.py: add IPv4-only SMTP subclasses that override_get_socket()instead of passing unsupportedsock=kwargs.gateway/platforms/email.py: close partially-open SMTP sessions ifSTARTTLSfails.tests/gateway/test_email.py: cover 465/587 protocol selection, IPv4 fallback, no TLS-error retry, and no global resolver mutation.Security notes
ssl.create_default_context().socket.getaddrinfoglobally.Validation
scripts/run_tests.sh tests/gateway/test_email.pypython -m py_compile gateway/platforms/email.py tests/gateway/test_email.pygit diff --checksmtplib.SMTP/SMTP_SSLhave nosock=kwarg; fallback uses subclassesInfographic
Original PR: #46061
Related duplicate: #46019
Fixes: #46018