From 5a8fa7cf31bf8a397a886eeab6c7a4a1a27d3a77 Mon Sep 17 00:00:00 2001 From: ksufer Date: Tue, 24 Feb 2026 19:18:55 +0800 Subject: [PATCH] Fix #13119: Use email.utils to fix IMAP parsing for names with commas (#13120) ## Type of Change - [x] Bug fix ## Description Closes #13119 The current IMAP connector uses `split(',')` to parse email headers, which crashes when a sender's display name contains a comma inside quotes (e.g., `"Doe, John" `). This PR replaces the manual string splitting with Python's standard `email.utils.getaddresses`. This correctly handles RFC 5322 quoted strings and prevents the `RuntimeError: Expected a singular address`. ## Checklist - [x] I have checked the code and it works as expected. --------- Co-authored-by: Kevin Hu --- common/data_source/imap_connector.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/common/data_source/imap_connector.py b/common/data_source/imap_connector.py index acaba7e01e..f682676e8e 100644 --- a/common/data_source/imap_connector.py +++ b/common/data_source/imap_connector.py @@ -8,7 +8,7 @@ import re from datetime import datetime, timedelta from datetime import timezone from email.message import Message -from email.utils import collapse_rfc2231_value, parseaddr +from email.utils import collapse_rfc2231_value, getaddresses from enum import Enum from typing import Any from typing import cast @@ -617,9 +617,9 @@ def _sanitize_mailbox_names(mailboxes: list[str]) -> list[str]: def _parse_addrs(raw_header: str) -> list[tuple[str, str]]: - addrs = raw_header.split(",") - name_addr_pairs = [parseaddr(addr=addr) for addr in addrs if addr] - return [(name, addr) for name, addr in name_addr_pairs if addr] + if not raw_header: + return [] + return getaddresses([raw_header]) def _parse_singular_addr(raw_header: str) -> tuple[str, str]: @@ -721,4 +721,4 @@ if __name__ == "__main__": start=START, end=END, ): - print(doc.id,doc.extension) \ No newline at end of file + print(doc.id,doc.extension)