Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions test_unstructured/partition/test_email_empty_body.py
Original file line number Diff line number Diff line change
@@ -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 ", "<html><body></body></html>"])
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("<p>The usable HTML body.</p>", 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("<p>HTML body.</p>", 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."]
14 changes: 14 additions & 0 deletions unstructured/partition/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down