MIME标头无法通过Gmail API实现 [英] MIME Headers Not Making it Through Gmail API

查看:89
本文介绍了MIME标头无法通过Gmail API实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过Gmail API自动创建草稿,我希望这些草稿是对现有电子邮件的回复.为此,我相信我需要设置"threadId"标头(特定于Gmail),"References"标头和"In-Reply-To"标头.此外,为使Gmail认为该邮件是答复,主题"标头必须与原始电子邮件匹配.

I'm trying to automate the creation of drafts via the Gmail API, and I want these drafts to be responses to existing emails. To do this, I believe I need to set the "threadId" header (Gmail specific), the "References" header, and the "In-Reply-To" header. Additionally, for Gmail to consider the message to be a reply, the "Subject" header must match the original email.

我正在将所有这些标头硬编码为MIMEText对象,然后将邮件作为字符串进行base-64编码(urlsafe),并让Gmail API进行传递.但是,"threadId","In-Reply-To"和"References"标头似乎从未出现在发送的电子邮件中,因为单击显示原始"时显示的MIME中不存在这些标头.在Gmail用户界面中.

I'm hardcoding all of these headers into a MIMEText object, and then base-64 encoding (urlsafe) the message as a string and having the Gmail API deliver it. However, the "threadId", "In-Reply-To", and "References" headers don't appear to ever make it in the email that's sent, as they don't exist in the MIME shown when clicking "Show original" in the Gmail UI.

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()

推荐答案

实际上,比这简单得多!如果您仅在标题中提供正确的Subject,在正文中提供正确的threadId,则Google会为您计算所有引用.

Actually, it's a lot simpler than that! If you just supply the correct Subject in the headers, and the correct threadId in the body, Google will calculate all the references for you.

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()

这将产生一个草稿,准备在正确的线程中发送:

This results in a draft, ready to be sent in the correct thread:

然后,我发送邮件.如您所见,引用是为您计算的:

Then, I send the mail. As you can see, the references are calculated for you:

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.

这篇关于MIME标头无法通过Gmail API实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆