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" <john@example.com>`).

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 <kevinhu.sh@gmail.com>
This commit is contained in:
ksufer
2026-02-24 19:18:55 +08:00
committed by GitHub
parent 0a7c520579
commit 5a8fa7cf31

View File

@@ -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)
print(doc.id,doc.extension)