gpt4 book ai didi

javascript - 如何使用 Gmail API 发送回复

转载 作者:数据小太阳 更新时间:2023-10-29 05:40:30 26 4
gpt4 key购买 nike

我有两个 Gmail 帐户,我创建了一个包含五封邮件的线程,并在此页面上使用 gmail gapi 检索它们 https://developers.google.com/gmail/api/v1/reference/users/threads/get .

这是我得到的:

enter image description here

如您所见,尽管 ID 标识完全相同的字母,但它们并不匹配。为什么会这样,如何获取统一的id?

附言我这样做的真正原因是我需要使用 gmail API 发送对消息的回复,但为此,您需要知道您回复的消息的 ID。如果我用我拥有的 ID(而不是接收者拥有的消息 ID)回复消息,它只会发送一条"new"消息。

如何使用 Gmail API 发送回复?

提前谢谢你。

最佳答案

作为docs say ,如果您正在尝试发送回复并希望电子邮件与主题串连,请确保:

  1. Subject header 匹配
  2. ReferencesIn-Reply-To header 遵循 RFC 2822 标准。

如果你想自己做,你可以得到你想要的消息的Subject, ReferencesMessage-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 还具有我们要响应的消息的值。我们还将 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>"
})
});

关于javascript - 如何使用 Gmail API 发送回复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32589476/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com