尝试使用 Gmail API 创建话题回复草稿时,草稿邮件中不会附加原始电子邮件内容



>标题说明了一切

我已经实现了使用 gmail api 文档为线程生成草稿。不过,它不起作用。

def create_message(sender, to, cc, bcc,inreplyto, subject, message_text, file=None, thread=None):
message = MIMEMultipart()
message['to'] = to
if cc:
message['cc'] = cc
if bcc:
message['bcc'] = bcc
message["In-Reply-To"] =inreplyto
message["References"] = inreplyto
message['from'] = sender
message['subject'] = subject
msg = MIMEText(message_text, 'html')
message.attach(msg)
#if file:
#message = attach_file(message, file)
output =  {'raw': base64.urlsafe_b64encode(message.as_string().encode()).decode()}
if thread:
output['threadId'] = thread
return output
#RFC format is followed as well

假设我的收件箱中有一封电子邮件"A"。使用 gmail API 的 Python 脚本会创建对此电子邮件的回复草稿,该回复将作为"B"存储在我的收件箱中。

预期 但是,我希望它以原始的gmail回复格式存储,即-

"B
..."

当您单击这三个点时,应出现原始电子邮件"A"

Gmail API 没有您想要的功能。您必须手动执行此操作。我尝试手动执行此操作,获取我要回复的消息对象并获取其正文数据。我将消息的正文数据与您跳跃的结构连接起来,如下所示:

def createBody(message_text, inreplyto, replyName, replyBody):
body = '<div dir="ltr">' 
+ message_text
+ '</div><br>'
+ '<div class="gmail_quote"><div dir="ltr" class="gmail_attr">'
+ 'On Thu, Jul 18, 2019 at 11:06 AM ' + replyName + ' &lt;<a href="mailto:' + inreplyto + '">' + inreplyto + '</a>&gt; wrote:<br></div>'
'<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">'
+ replyBody
+ '</div>'
+ '</blockquote>'

您可以使用 Gmail 文档 [1] 中的"试用此 API"功能自行检查此结构。

我将这段代码放在您的 create_message 函数中,以将手动修改的正文设置为草稿消息:

#create body
message_text = createBody(message_text, inreplyto, "name", replyBody)
msg = MIMEText(message_text, 'html')
message.attach(msg)

在这里,我得到我要回复的消息和消息的正文:

replyBody = base64.b64decode(service.users().messages().get(userId="me", id="XXXX").execute()['payload']['parts'][1]['body']['data']+'==')

对我来说,它适用于结构化草稿,但我无法正确解码 replyBody 数据(在在线工具中确实正确解码,但在 Python 中没有,不知道为什么)。另一件悬而未决的事情是找到一种方法来获取您要回复的人的姓名。

[1] https://developers.google.com/gmail/api/v1/reference/users/messages/get

最新更新