Skip to content

The agent engineering platform.

fix multipart email body retrieval (#9790)

Juhee Kim50ca44c79ffa
diff --git a/libs/langchain/langchain/tools/gmail/get_message.py b/libs/langchain/langchain/tools/gmail/get_message.py
index e1cca7b13e..a5f35ba71e 100644
--- a/libs/langchain/langchain/tools/gmail/get_message.py
+++ b/libs/langchain/langchain/tools/gmail/get_message.py
@@ -46,7 +46,16 @@ class GmailGetMessage(GmailBaseTool):
subject = email_msg["Subject"]
sender = email_msg["From"]
- message_body = email_msg.get_payload()
+ message_body = ""
+ if email_msg.is_multipart():
+ for part in email_msg.walk():
+ ctype = part.get_content_type()
+ cdispo = str(part.get("Content-Disposition"))
+ if ctype == "text/plain" and "attachment" not in cdispo:
+ message_body = part.get_payload(decode=True).decode("utf-8")
+ break
+ else:
+ message_body = email_msg.get_payload(decode=True).decode("utf-8")
body = clean_email_body(message_body)
diff --git a/libs/langchain/langchain/tools/gmail/search.py b/libs/langchain/langchain/tools/gmail/search.py
index 045834abc1..f4de000f51 100644
--- a/libs/langchain/langchain/tools/gmail/search.py
+++ b/libs/langchain/langchain/tools/gmail/search.py
@@ -91,7 +91,16 @@ class GmailSearch(GmailBaseTool):
subject = email_msg["Subject"]
sender = email_msg["From"]
- message_body = email_msg.get_payload()
+ message_body = ""
+ if email_msg.is_multipart():
+ for part in email_msg.walk():
+ ctype = part.get_content_type()
+ cdispo = str(part.get("Content-Disposition"))
+ if ctype == "text/plain" and "attachment" not in cdispo:
+ message_body = part.get_payload(decode=True).decode("utf-8")
+ break
+ else:
+ message_body = email_msg.get_payload(decode=True).decode("utf-8")
body = clean_email_body(message_body)