From 50ca44c79ffab9e29ecc5e154b4c833cd0893a32 Mon Sep 17 00:00:00 2001 From: Juhee Kim <46583939+juppytt@users.noreply.github.com> Date: Mon, 4 Sep 2023 08:04:36 +0900 Subject: [PATCH] fix multipart email body retrieval (#9790) Description: Gmail message retrieval in GmailGetMessage and GmailSearch returned an empty string when encountering multipart emails. This change correctly extracts the email body for multipart emails. Dependencies: None @hwchase17 @vowelparrot --- libs/langchain/langchain/tools/gmail/get_message.py | 11 ++++++++++- libs/langchain/langchain/tools/gmail/search.py | 11 ++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/libs/langchain/langchain/tools/gmail/get_message.py b/libs/langchain/langchain/tools/gmail/get_message.py index e1cca7b13..a5f35ba71 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 045834abc..f4de000f5 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)