Skip to content

fix(email): keep the body apart from the text of an attached message - #5590

Open
Mani7527 wants to merge 2 commits into
docling-project:mainfrom
Mani7527:fix/email-attached-message-body
Open

Mani7527 wants to merge 2 commits into
docling-project:mainfrom
Mani7527:fix/email-attached-message-body

Conversation

@Mani7527

@Mani7527 Mani7527 commented Nov 25, 2026 •

Copy link
Copy Markdown

When an email has an HTML body and a forwarded message attached (message/rfc822), the converted document shows only the forwarded message's text, and the real body is lost:

Content-Type: multipart/mixed; boundary="B"

--B
Content-Type: text/html; charset=utf-8

<html><body><p>See the forwarded message.</p></body></html>
--B
Content-Type: message/rfc822
Content-Disposition: attachment; filename="forwarded.eml"

From: bob@example.com
Subject: Original
Content-Type: text/plain

Original text.
--B--
  • Before: the body is Original text.; See the forwarded message. is missing.
  • After: the body is See the forwarded message.

mailparser adds the unnamed text parts of an attached message/rfc822 (or of an attachment container) to text_plain / text_html. _get_body_paragraphs uses text_plain first whenever it is non-empty, so the inner plain text won and the outer HTML body was never read. With a plain-text outer body, the forwarded text was mixed into the body.

The backend already walked the MIME tree to find the format=flowed parameters of each text_plain part (_get_plain_text_flows). That walk now also marks the parts inside an attachment container, such as a message/rfc822 attachment, as embedded (_get_text_body_parts, for both plain and HTML), and the body skips them. An inline message/rfc822 part (no disposition or inline) and the messages of a multipart/digest are not attachments and stay in the body, as on main. If the part count doesn't match mailparser's list, all texts are kept as before. A message that only contains an attached message still falls back to mail.body.

Checklist:

  • Documentation has been updated, if necessary.
  • Examples have been added, if necessary.
  • Tests have been added, if necessary.

Review evidence:

  • New tests in tests/test_backend_email_forwarded.py (a separate file, as test_backend_email.py would exceed the 1500-line limit):
    • test_email_backend_keeps_body_apart_from_attached_message (HTML or plain-text body + message/rfc822 attachment): fails on main, passes with the change.
    • test_email_backend_keeps_inline_message_in_body (no disposition / inline) and test_email_backend_keeps_digest_messages_in_body: pass on main and with the change, guarding the inline behaviour.
  • tests/test_backend_email*.py and tests/test_backend_calendar*.py: 119 passed.
  • ruff check / ruff format --check clean. make validate and the tach hooks were not run locally.
  • No reference data changed.
  • Note: open PRs feat(email): read the HTML body of .msg files from the RTF body and add prefer_html #5465 and fix(email): preserve HTML body structure #5324 also touch _get_body_paragraphs; happy to rebase on whichever lands first.

mailparser adds the unnamed text parts of an attached message/rfc822 (or
of an attachment container) to text_plain and text_html. The backend
read them as body text. When the message had an HTML body and the
attached message a plain-text part, the plain text was preferred and the
real HTML body was dropped, so the document showed only the text of the
forwarded message.

Mark the text parts inside an attachment or an attached message as
embedded and leave them out of the body.

Signed-off-by: Manikesh <Mani7527@users.noreply.github.com>
@github-actions

Copy link
Copy Markdown
Contributor

✅ DCO Check Passed

Thanks @Mani7527, all your commits are properly signed off. 🎉

@mergify

mergify Bot commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Merge Protections

🟢 Merge protection satisfied — ready to merge.

Show 1 satisfied protection

🟢 Enforce conventional commit

Make sure that we follow https://www.conventionalcommits.org/en/v1.0.0/

  • title ~= ^(fix|feat|docs|style|refactor|perf|test|build|ci|chore|revert)(?:\(.+\))?(!)?:

@github-actions github-actions Bot added bug Something isn't working email issue related to email (EML, MSG) backend labels Nov 25, 2026
@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

AI triage (advisory)

Checked commit a5ac347acd. The analysis read the diff only. It did not run code from this pull request.

The email backend now skips text parts that sit inside an attachment, such as a message/rfc822 attachment, when it builds the body. This keeps the real HTML or plain-text body from being replaced by, or mixed with, the forwarded message text.

CI safety: no concern found. A maintainer must still approve CI.

2 related pull request(s) in the same code
PR State Signal Assessment
#5465 open referenced as #5465 related: It also changes _get_body_paragraphs in email_backend.py. It adds an RTF HTML body for .msg files and a prefer_html option. It does not fix the attached-message body problem.
#5324 open referenced as #5324 related: It also changes the body code in email_backend.py. It keeps the HTML structure and the heading levels. It does not skip text parts from attached messages.

The ai:* labels are advisory. They do not approve CI and they do not add tests:full. A new push runs this check again.

@codecov

codecov Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.55172% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
docling/backend/email_backend.py 96.55% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@dolfim-ibm dolfim-ibm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One blocker, and a suggestion about the root cause.

Blocker: inline message/rfc822 parts are now dropped, with no trace in the output

The new elif embedded or part.get_content_maintype() == "message" branch in _get_text_body_parts marks every attached message as embedded, also when it has no Content-Disposition or has Content-Disposition: inline. Such a part is not an attachment: mailparser does not list it in attachments, and mail clients show it inline. So its text leaves the body and does not appear as an attachment label either. The new no-disposition test case asserts this loss as the expected result.

This is a regression for messages that convert correctly on main. A plain-text body with an inline forwarded message:

main:     ['From: alice@example.com', 'Plain body.', 'Original text.']
PR head:  ['From: alice@example.com', 'Plain body.']

A MIME digest (Mailman digests use multipart/digest, whose children are message/rfc822 with no disposition) loses all the digested messages:

main:     ["Today's Topics: ...", 'We ship on Friday.', 'Friday works for me.', 'End of Dev Digest']
PR head:  ["Today's Topics: ...", 'End of Dev Digest']

Suggested change: mark as embedded only the descendants of attachment containers (the first branch, which mirrors mailparser's blocks_inline rule), and drop the message maintype branch. The reported case has Content-Disposition: attachment; filename="forwarded.eml", so it stays fixed. With list_attachments=True it is still listed as forwarded.eml (message/rfc822). Please change the no-disposition test case to expect the forwarded text in the body.

Suggestion: the root cause is the global plain-before-HTML choice

_get_body_paragraphs uses all of text_plain when it has any text, and reads text_html only when it does not. Plain and HTML are alternatives only inside multipart/alternative. In multipart/mixed they are separate content. So any unrelated text/plain part still hides an HTML body on the PR head. For example, an HTML body plus the text/plain footer part that Mailman appends:

main and PR head:  ['From: alice@example.com', '_______\nList footer.']   # 'HTML body.' is lost

This is shared with main and does not block this PR. A complete fix walks the MIME tree once, picks one alternative per multipart/alternative, keeps the other parts in document order, skips attachment containers, and renders an inline message/rfc822 as a nested section with its own headers. The backend already walks the tree for the flowed parameters and the calendar parts, and matches mailparser's lists by position. #5465 (prefer_html) and #5324 also change _get_body_paragraphs, so the three changes are best aligned on that one selection step. That can be a follow-up issue.

Validation on 1695e7c7

  • The new test fails on the merge base 33f33aaf (4 failed) and passes on the head.
  • tests/test_backend_email*.py and tests/test_backend_calendar*.py: 118 passed.
  • make check: passed.

Only the text parts inside an attachment container are left out of the
body now. A message/rfc822 part without an attachment disposition, such
as an inline forwarded message or the messages of a multipart/digest, is
not an attachment and keeps its text in the body, as on main.

Move the forwarded-message tests to test_backend_email_forwarded.py to
keep test_backend_email.py under the line limit.

Signed-off-by: Manikesh <Mani7527@users.noreply.github.com>
@Mani7527

Copy link
Copy Markdown
Author

Thanks for the review and the examples. Fixed in a5ac347:

  • Dropped the message maintype branch, so only the descendants of attachment containers are marked as embedded. An inline or no-disposition message/rfc822 part and the messages of a multipart/digest stay in the body, as on main. The reported case (Content-Disposition: attachment; filename="forwarded.eml") stays fixed.
  • Tests are in tests/test_backend_email_forwarded.py, since test_backend_email.py would go over the 1500-line limit. ..._keeps_body_apart_from_attached_message fails on main and passes now; ..._keeps_inline_message_in_body and ..._keeps_digest_messages_in_body fail on the previous head 1695e7c and pass now.
  • tests/test_backend_email*.py and tests/test_backend_calendar*.py: 119 passed.

Agreed that the plain-before-HTML selection is the root cause. Happy to open a follow-up issue for the single MIME-walk approach so it can be aligned with #5465 and #5324.

@github-actions github-actions Bot added the ai:ci-safe AI triage found no CI-safety concern (advisory) label Nov 26, 2026
@Mani7527
Mani7527 requested a review from dolfim-ibm November 28, 2026 19:30

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai:ci-safe AI triage found no CI-safety concern (advisory) bug Something isn't working email issue related to email (EML, MSG) backend

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants