Skip to content

The agent that grows with you

fix(email): send IMAP ID only when the server advertises the capability

Rob Aleck25be5e212054
diff --git a/plugins/platforms/email/adapter.py b/plugins/platforms/email/adapter.py
index 5f68843c417..efcc6238978 100644
--- a/plugins/platforms/email/adapter.py
+++ b/plugins/platforms/email/adapter.py
@@ -141,7 +141,19 @@ def _open_smtp(host: str, port: int, security: str, ctx: ssl.SSLContext, smtp_cl
def _send_imap_id(imap: "imaplib.IMAP4") -> None:
"""Send RFC 2971 IMAP ID: 163/NetEase require it after LOGIN (else every UID command
- returns ``BYE Unsafe Login``); other servers may reject it, so failures are swallowed."""
+ returns ``BYE Unsafe Login``); other servers may reject it, so failures are swallowed.
+
+ Sent only when the server advertises ``ID`` (RFC 2971 requires advertising it): a server
+ without the extension can answer an untagged ``* BYE Unknown command.`` and close the
+ connection, which imaplib cannot surface here — the failure appears one command later
+ as a misleading SELECT error and the adapter retries forever (Purelymail, #39856).
+ ``imap.capabilities`` is populated by imaplib at connect, so the check is free."""
+ caps = getattr(imap, "capabilities", ()) or ()
+ if "ID" not in caps:
+ logger.debug(
+ "[Email] Server does not advertise IMAP ID capability; skipping ID"
+ )
+ return
try:
try:
from hermes_cli import __version__ as _hermes_version
diff --git a/tests/gateway/test_email.py b/tests/gateway/test_email.py
index e0ea57ce3bc..07278a7b84e 100644
--- a/tests/gateway/test_email.py
+++ b/tests/gateway/test_email.py
@@ -939,6 +939,7 @@ class TestImapIdExtensionForNetEase(unittest.TestCase):
adapter = self._make_adapter()
mock_imap = MagicMock()
+ mock_imap.capabilities = ("IMAP4REV1", "ID", "UIDPLUS")
mock_imap.uid.return_value = ("OK", [b""])
with patch("imaplib.IMAP4_SSL", return_value=mock_imap), \
@@ -962,6 +963,56 @@ class TestImapIdExtensionForNetEase(unittest.TestCase):
self.assertIn("login", names)
self.assertLess(names.index("login"), names.index("xatom"))
+ def test_send_imap_id_sent_when_capability_advertised(self):
+ """ID goes out when the server lists it in CAPABILITY."""
+ from plugins.platforms.email.adapter import _send_imap_id
+
+ mock_imap = MagicMock()
+ mock_imap.capabilities = ("IMAP4REV1", "ID", "UIDPLUS")
+
+ _send_imap_id(mock_imap)
+
+ mock_imap.xatom.assert_called_once()
+ self.assertEqual(mock_imap.xatom.call_args.args[0], "ID")
+
+ def test_send_imap_id_skipped_when_capability_absent(self):
+ """Servers that do not advertise ID must not receive it: Purelymail
+ answers the unknown command with an untagged ``* BYE Unknown
+ command.`` and drops the connection, which imaplib surfaces one
+ command later as a misleading SELECT failure."""
+ from plugins.platforms.email.adapter import _send_imap_id
+
+ mock_imap = MagicMock()
+ mock_imap.capabilities = ("IMAP4REV1", "UIDPLUS")
+
+ _send_imap_id(mock_imap)
+
+ mock_imap.xatom.assert_not_called()
+
+ def test_send_imap_id_skipped_when_capabilities_attribute_missing(self):
+ """A connection object without a capabilities attribute must not
+ crash the helper; fail toward not sending optional commands."""
+ from plugins.platforms.email.adapter import _send_imap_id
+
+ mock_imap = MagicMock(spec=["xatom"])
+
+ _send_imap_id(mock_imap)
+
+ mock_imap.xatom.assert_not_called()
+
+ def test_send_imap_id_rejection_still_swallowed(self):
+ """A server that advertises ID but rejects it with a tagged error
+ keeps the existing best-effort handling: no exception escapes."""
+ from plugins.platforms.email.adapter import _send_imap_id
+
+ mock_imap = MagicMock()
+ mock_imap.capabilities = ("IMAP4REV1", "ID")
+ mock_imap.xatom.side_effect = Exception("BAD ID rejected")
+
+ _send_imap_id(mock_imap) # must not raise
+
+ mock_imap.xatom.assert_called_once()
+
class TestConnectSmtp(unittest.TestCase):
"""Test _connect_smtp() helper: protocol selection and IPv6 fallback."""