根据文档,如果您想要发送回复并使邮件进入同一线程,请确保:
Subject
标题相同。
References
和In-Reply-To
头遵循RFC 2822标准。
如果您想自己完成此操作,可以获取要回复的消息的Subject
、References
和Message-ID
头:
请求:
userId = me
id = 14fd1c555a1352b7 // id of the message I want to respond to.
format = metadata
metadataHeaders = Subject,References,Message-ID
GET https://www.googleapis.com/gmail/v1/users/me/messages/14fd1c555a1352b7?format=metadata&metadataHeaders=Subject&metadataHeaders=References&metadataHeaders=Message-ID
响应:
{
"id": "14fd1c555a1352b7",
"threadId": "14fd1c52911f0f64",
"labelIds": [
"SENT",
"INBOX",
"IMPORTANT",
"UNREAD"
],
"snippet": "Next level dude 2015-09-15 18:10 GMT+02:00 Emil Tholin <emtholin@gmail.com>: wow 2015-09-15 18:",
"historyId": "575289",
"internalDate": "1442333414000",
"payload": {
"mimeType": "multipart/alternative",
"headers": [
{
"name": "In-Reply-To",
"value": "<CADsZLRyzVPLRQuTthGSHKMCXL7Ora1jNW7h0jvoNgR+hU59BYg@mail.gmail.com>"
},
{
"name": "References",
"value": "<CADsZLRxZDUGn4Frx80qe2_bE5H5bQhgcqGk=GwFN9gs7Z_8oZw@mail.gmail.com> <CADsZLRyzVPLRQuTthGSHKMCXL7Ora1jNW7h0jvoNgR+hU59BYg@mail.gmail.com>"
},
{
"name": "Message-ID",
"value": "<CADsZLRwQWzLB-uq4_4G2E64NX9G6grn0cEeO0L=avY7ajzuAFg@mail.gmail.com>"
},
{
"name": "Subject",
"value": "Re: Cool"
}
]
},
"sizeEstimate": 1890
}
为遵循RFC2822标准,我们已将要回复的邮件的Message-ID添加到References标头中,并用空格分隔。In-Reply-To标头也具有我们要回复的邮件的值。我们还在Subject标头中添加了Re:以表示这是一封回复邮件。
var encodedResponse = btoa(
"Content-Type: text/plain; charset=\"UTF-8\"\n" +
"MIME-Version: 1.0\n" +
"Content-Transfer-Encoding: 7bit\n" +
"References: <CADsZLRxZDUGn4Frx80qe2_bE5H5bQhgcqGk=GwFN9gs7Z_8oZw@mail.gmail.com> <CADsZLRyzVPLRQuTthGSHKMCXL7Ora1jNW7h0jvoNgR+hU59BYg@mail.gmail.com> <CADsZLRwQWzLB-uq4_4G2E64NX9G6grn0cEeO0L=avY7ajzuAFg@mail.gmail.com>\n" +
"In-Reply-To: <CADsZLRwQWzLB-uq4_4G2E64NX9G6grn0cEeO0L=avY7ajzuAFg@mail.gmail.com>\n" +
"Subject: Re:Cool\n" +
"From: sender@gmail.com\n" +
"To: reciever@gmail.com\n\n" +
"This is where the response text will go"
).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
$.ajax({
url: "https://www.googleapis.com/gmail/v1/users/me/messages/send?access_token=<USER_ACCESS_TOKEN>",
method: "POST",
contentType: "application/json",
data: JSON.stringify({
raw: encodedResponse
})
});
正如你所看到的,这是一个手动繁琐的过程。你也可以简单地回复该线程。然而,这可能不足以满足你的使用情况。
通过这种方式,你只需要提供邮件和threadId
,并确保Subject
相同,Google会正确地为你显示它。
var encodedResponse = btoa(
"Content-Type: text/plain; charset=\"UTF-8\"\n" +
"MIME-Version: 1.0\n" +
"Content-Transfer-Encoding: 7bit\n" +
"Subject: Subject of the original mail\n" +
"From: sender@gmail.com\n" +
"To: reciever@gmail.com\n\n" +
"This is where the response text will go"
).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
$.ajax({
url: "https://www.googleapis.com/gmail/v1/users/me/messages/send?access_token=<USER_ACCESS_TOKEN>",
method: "POST",
contentType: "application/json",
data: JSON.stringify({
raw: encodedResponse,
threadId: "<THREAD_ID_OF_MESSAGE_TO_RESPOND_TO>"
})
});