From 88de740fc05a50ab1d00df238489459e2753a5b1 Mon Sep 17 00:00:00 2001 From: Christopher Krah <48338417+chrikrah@users.noreply.github.com> Date: Tue, 20 Oct 2026 05:00:12 +0200 Subject: [PATCH] fix(email): drop the empty entry of an address group An RFC 5322 group with no members, such as "To: undisclosed-recipients:;", comes back from email.utils.getaddresses() as ("", ""), so sent_to, cc_recipient and bcc_recipient held [""]. Drop entries without an address in one helper used by all four address properties. Signed-off-by: Christopher Krah <48338417+chrikrah@users.noreply.github.com> --- CHANGELOG.md | 6 ++++++ test_unstructured/partition/test_email.py | 17 +++++++++++++++ unstructured/__version__.py | 2 +- unstructured/partition/email.py | 26 +++++++++++++++-------- 4 files changed, 41 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c8b628d0e2..dddd06fd58 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## 0.27.31 + +### Fixes + +- **Drop the empty entry of an email address group.** `partition_email()` reported a header such as `To: undisclosed-recipients:;`, an RFC 5322 group with no members, as `sent_to=[""]`, and the same for `cc_recipient` and `bcc_recipient`. Entries without an address are now dropped, so such a header gives `None`, and a group with members gives only their addresses. + ## 0.27.30 ### Fixes diff --git a/test_unstructured/partition/test_email.py b/test_unstructured/partition/test_email.py index 6fd0f82f38..1faa963699 100644 --- a/test_unstructured/partition/test_email.py +++ b/test_unstructured/partition/test_email.py @@ -256,6 +256,23 @@ def test_partition_email_accepts_non_ascii_addresses(): assert element.metadata.sent_to == ["Jürgen ", "alice@example.com"] +def test_partition_email_drops_the_empty_entry_of_an_address_group(): + # -- RFC 5322 group syntax: "undisclosed-recipients:;" names a group with no members -- + msg = EmailMessage() + msg["From"] = "alice@example.com" + msg["To"] = "undisclosed-recipients:;" + msg["Cc"] = "Team: bob@example.com, carol@example.com;" + msg["Bcc"] = "undisclosed-recipients:;" + msg.set_content("Hello") + + element = partition_email(file=io.BytesIO(msg.as_bytes()))[0] + + assert element.metadata.sent_from == ["alice@example.com"] + assert element.metadata.sent_to is None + assert element.metadata.cc_recipient == ["bob@example.com", "carol@example.com"] + assert element.metadata.bcc_recipient is None + + # -- .metadata.filename -------------------------------------------------------------------------- diff --git a/unstructured/__version__.py b/unstructured/__version__.py index 1c0cb5ae0a..86af1c7114 100644 --- a/unstructured/__version__.py +++ b/unstructured/__version__.py @@ -1 +1 @@ -__version__ = "0.27.30" # pragma: no cover +__version__ = "0.27.31" # pragma: no cover diff --git a/unstructured/partition/email.py b/unstructured/partition/email.py index 6a4c5252d5..d47941db79 100644 --- a/unstructured/partition/email.py +++ b/unstructured/partition/email.py @@ -53,6 +53,18 @@ def _format_address(name: str, address: str) -> str: return f"{quotes}{name}{quotes} <{address}>" +def _format_addresses(headers: list[str]) -> list[str] | None: + """The addresses in `headers`, formatted, or None when there are none. + + An empty group such as "undisclosed-recipients:;" holds no address, and `getaddresses()` + reports it as `("", "")`, so entries without an address are dropped. + """ + addrs = [ + _format_address(name, addr) for name, addr in email.utils.getaddresses(headers) if addr + ] + return addrs or None + + @partition_runtime_telemetry("eml") def partition_email( filename: str | None = None, @@ -151,8 +163,7 @@ def bcc_addresses(self) -> list[str] | None: bccs = self.msg.get_all("Bcc") if not bccs: return None - addrs = email.utils.getaddresses(bccs) - return [_format_address(*addr) for addr in addrs] + return _format_addresses(bccs) @cached_property def body_part(self) -> MIMEPart | None: @@ -169,8 +180,7 @@ def cc_addresses(self) -> list[str] | None: ccs = self.msg.get_all("Cc") if not ccs: return None - addrs = email.utils.getaddresses(ccs) - return [_format_address(*addr) for addr in addrs] + return _format_addresses(ccs) @cached_property def content_type_preference(self) -> tuple[str, ...]: @@ -204,9 +214,8 @@ def from_address(self) -> str | None: if not froms: # -- this should never occur because the From: header is mandatory per RFC 5322 -- return None - addrs = email.utils.getaddresses(froms) - formatted_addrs = [_format_address(*addr) for addr in addrs] - return formatted_addrs[0] + formatted_addrs = _format_addresses(froms) + return formatted_addrs[0] if formatted_addrs else None @cached_property def message_id(self) -> str | None: @@ -294,8 +303,7 @@ def to_addresses(self) -> list[str] | None: tos = self.msg.get_all("To") if not tos: return None - addrs = email.utils.getaddresses(tos) - return [_format_address(*addr) for addr in addrs] + return _format_addresses(tos) @cached_property def _filesystem_last_modified(self) -> str | None: