fix(email): exclude attached messages from body extraction

This commit is contained in:
fhgffy authored and Xubin Ren committed 2026-10-18 19:11:29 +08:00
1 parent 271bda1d81
commit aef8127aff
2 files changed
+32 -1

No files matched your search

+6 -1
View File
@@ -845,9 +845,14 @@ class EmailChannel(BaseChannel):
if msg.is_multipart():
plain_parts: list[str] = []
html_parts: list[str] = []
for part in msg.walk():
parts = [msg]
while parts:
part = parts.pop()
if part.get_content_disposition() == "attachment":
continue
if part.is_multipart():
parts.extend(reversed(part.get_payload()))
continue
content_type = part.get_content_type()
try:
payload = part.get_content()
@@ -0,0 +1,26 @@
from email import policy
from email.message import EmailMessage
from email.parser import BytesParser
from nanobot.channels.email.runtime import EmailChannel
def test_attached_email_is_not_included_in_message_body():
message = EmailMessage()
message.set_content("Please review the attached email.")
attachment = EmailMessage()
attachment["Subject"] = "Forwarded conversation"
attachment.set_content("This belongs to the attached conversation.")
message.add_attachment(attachment, filename="forwarded.eml")
parsed = BytesParser(policy=policy.default).parsebytes(message.as_bytes())
assert EmailChannel._extract_text_body(parsed) == "Please review the attached email."
def test_multipart_alternative_still_prefers_plain_text():
message = EmailMessage()
message.set_content("Plain body.")
message.add_alternative("<p>HTML body.</p>", subtype="html")
parsed = BytesParser(policy=policy.default).parsebytes(message.as_bytes())
assert EmailChannel._extract_text_body(parsed) == "Plain body."