Repository navigation
Conversation
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>
|
✅ DCO Check Passed Thanks @Mani7527, all your commits are properly signed off. 🎉 |
Merge Protections🟢 Merge protection satisfied — ready to merge. Show 1 satisfied protection🟢 Enforce conventional commitMake sure that we follow https://www.conventionalcommits.org/en/v1.0.0/
|
AI triage (advisory)Checked commit The email backend now skips text parts that sit inside an attachment, such as a CI safety: no concern found. A maintainer must still approve CI. 2 related pull request(s) in the same code
The |
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
dolfim-ibm
left a comment
There was a problem hiding this comment.
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*.pyandtests/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>
|
Thanks for the review and the examples. Fixed in a5ac347:
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. |
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:Original text.;See the forwarded message.is missing.See the forwarded message.mailparser adds the unnamed text parts of an attached
message/rfc822(or of an attachment container) totext_plain/text_html._get_body_paragraphsusestext_plainfirst 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_plainpart (_get_plain_text_flows). That walk now also marks the parts inside an attachment container, such as amessage/rfc822attachment, as embedded (_get_text_body_parts, for both plain and HTML), and the body skips them. An inlinemessage/rfc822part (no disposition orinline) and the messages of amultipart/digestare 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 tomail.body.Checklist:
Review evidence:
tests/test_backend_email_forwarded.py(a separate file, astest_backend_email.pywould exceed the 1500-line limit):test_email_backend_keeps_body_apart_from_attached_message(HTML or plain-text body +message/rfc822attachment): fails on main, passes with the change.test_email_backend_keeps_inline_message_in_body(no disposition /inline) andtest_email_backend_keeps_digest_messages_in_body: pass on main and with the change, guarding the inline behaviour.tests/test_backend_email*.pyandtests/test_backend_calendar*.py: 119 passed.ruff check/ruff format --checkclean.make validateand the tach hooks were not run locally._get_body_paragraphs; happy to rebase on whichever lands first.