繁体   English   中英

MIME标头无法通过Gmail API实现

[英]MIME Headers Not Making it Through Gmail API

我正在尝试通过Gmail API自动创建草稿,我希望这些草稿能够回复现有的电子邮件。 为此,我认为我需要设置“threadId”标题(特定于Gmail),“引用”标题和“In-Reply-To”标题。 此外,要让Gmail将邮件视为回复,“主题”标题​​必须与原始电子邮件匹配。

我将所有这些标头硬编码到MIMEText对象中,然后将消息作为字符串进行base-64编码(urlsafe)并让Gmail API提供它。 但是,“threadId”,“In-Reply-To”和“References”标题似乎没有在发送的电子邮件中显示,因为单击“显示原始”时显示的MIME中不存在它们在Gmail用户界面中。

new = MIMEText("reply body text")
new["In-Reply-To"] = "[Message-ID of email to reply to]" #looks like <..@mail.gmail.com>
new["References"] = "[Message-ID of email to reply to]" #looks like <..@mail.gmail.com>
new["threadId"] = "[threadId of message to reply to]" #looks like 14ec476abbce3421
new["Subject"] = "Testsend2"
new["To"] = "[Email to send to]"
new["From"] = "[Email to send from]"

messageToDraft = {'raw': base64.urlsafe_b64encode(new.as_string())}
message = {'message': messageToDraft}
draft = service.users().drafts().create(userId="me", body=message).execute()

实际上,它比这简单得多! 如果您只在标题中提供正确的主题,并在正文中提供正确的threadId,Google将为您计算所有参考。

new = MIMEText("This is the placeholder draft message text.")
new["Subject"] = "Example Mail"
new["To"] = "emtholin@gmail.com"
new["From"] = "emtholin@gmail.com"

raw = base64.urlsafe_b64encode(new.as_string())
message = {'message': {'raw': raw, 'threadId': "14ec598be7f25362"}}
draft = service.users().drafts().create(userId="me", body=message).execute()

这导致草稿,准备好在正确的线程中发送:

在此输入图像描述

然后,我发送邮件。 如您所见,参考资料是为您计算的:

MIME-Version: 1.0
Received: by 10.28.130.132 with HTTP; Sat, 25 Jul 2015 07:54:12 -0700 (PDT)
In-Reply-To: <CADsZLRz5jWF5h=6Cs1F45QQOiFuqNGmMeb6St5e-tOj3stCNiA@mail.gmail.com>
References: <CADsZLRwmDZ_L5_zWqE8qOgoKuvRiRTWUopqssn4+XYGM_SKrfg@mail.gmail.com>
    <CADsZLRz5jWF5h=6Cs1F45QQOiFuqNGmMeb6St5e-tOj3stCNiA@mail.gmail.com>
Date: Sat, 25 Jul 2015 16:54:12 +0200
Delivered-To: emtholin@gmail.com
Message-ID: <CADsZLRxuyFhuGNPwjRrfFVQ0_2MxO=_jstjmsBGmAiwMEvfWSg@mail.gmail.com>
Subject: Example Mail
From: Emil Tholin <emtholin@gmail.com>
To: Emil Tholin <emtholin@gmail.com>
Content-Type: text/plain; charset=UTF-8

This is the placeholder draft message text.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM