正如文档所说,如果您尝试发送回复并希望将电子邮件发送出去,请确保:
- 该
Subject
标题匹配
- 在
References
与In-Reply-To
头遵循RFC 2822标准。
如果您想自己执行此操作,则可以获得要回复的消息的Subject
,References
和Message-ID
-headers :
要求:
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", // This is the same for both users, as you were asking about.
"value": "<CADsZLRwQWzLB-uq4_4G2E64NX9G6grn0cEeO0L=avY7ajzuAFg@mail.gmail.com>"
},
{
"name": "Subject",
"value": "Re: Cool"
}
]
},
"sizeEstimate": 1890
}
为了遵循 RFC 2822 标准,我们添加了Message-ID
我们想要响应的消息的References
-header,用空格分隔。该In-Reply-To
-header也有,我们要回应消息的value。我们还添加Re:
到我们的Subject
-header 以表明它是一个响应。
// Base64-encode the mail and make it URL-safe
// (replace "+" with "-", replace "/" with "_", remove trailing "=")
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 就会为您正确显示。
// Base64-encode the mail and make it URL-safe
// (replace "+" with "-", replace "/" with "_", remove trailing "=")
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>"
})
});