diff --git a/test_unstructured/partition/test_email_empty_body.py b/test_unstructured/partition/test_email_empty_body.py new file mode 100644 index 0000000000..bf50e63ad0 --- /dev/null +++ b/test_unstructured/partition/test_email_empty_body.py @@ -0,0 +1,48 @@ +import io +from email.message import EmailMessage + +import pytest + +from unstructured.partition.email import partition_email + + +@pytest.mark.parametrize("html", ["", " \n ", "
"]) +def test_empty_preferred_html_does_not_hide_the_plain_body(html): + message = EmailMessage() + message["Subject"] = "Body fallback" + message.set_content("The usable plain text body.") + message.add_alternative(html, subtype="html") + + elements = partition_email( + file=io.BytesIO(message.as_bytes()), process_attachments=False, languages=["eng"] + ) + + assert [e.text for e in elements] == ["The usable plain text body."] + assert elements[0].metadata.subject == "Body fallback" + + +def test_empty_preferred_plain_text_falls_back_to_html(): + message = EmailMessage() + message.set_content("") + message.add_alternative("The usable HTML body.
", subtype="html") + + elements = partition_email( + file=io.BytesIO(message.as_bytes()), + content_source="text/plain", + process_attachments=False, + languages=["eng"], + ) + + assert [e.text for e in elements] == ["The usable HTML body."] + + +def test_nonempty_preference_is_kept_and_alternatives_are_not_duplicated(): + message = EmailMessage() + message.set_content("Plain body.") + message.add_alternative("HTML body.
", subtype="html") + + elements = partition_email( + file=io.BytesIO(message.as_bytes()), process_attachments=False, languages=["eng"] + ) + + assert [e.text for e in elements] == ["HTML body."] diff --git a/unstructured/partition/email.py b/unstructured/partition/email.py index db6ed5bf8f..51951a70c5 100644 --- a/unstructured/partition/email.py +++ b/unstructured/partition/email.py @@ -382,6 +382,20 @@ def _iter_email_body_elements(self) -> Iterator[Element]: if body_part is None: return + elements = list(self._iter_body_part_elements(body_part)) + if elements: + yield from elements + return + + # An empty preferred alternative must not hide a usable rendition of the message. + # get_body still excludes attachment parts when selecting the fallback. + other_type = "plain" if body_part.get_content_type() == "text/html" else "html" + alternative = self._ctx.msg.get_body(preferencelist=(other_type,)) + if alternative is not None and alternative is not body_part: + yield from self._iter_body_part_elements(alternative) + + def _iter_body_part_elements(self, body_part: MIMEPart) -> Iterator[Element]: + """Partition one selected text body without duplicating MIME alternatives.""" content_type = body_part.get_content_type() content = body_part.get_content() assert isinstance(content, str)