如何使用Gmail API发送回复 [英] How To Send A Reply With Gmail API

查看:158
本文介绍了如何使用Gmail API发送回复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个Gmail帐户,我创建了一个包含五条消息的线程,并在此页面使用gmail gapi将其回收



正如你所看到的,ID不匹配,虽然它们确定了完全相同的字母。为什么会发生这种情况,我该如何获得统一的身份证?



我这样做的真正原因是我需要使用gmail API发送消息回复,但要做到这一点,您需要知道您回复的消息的ID。如果我回复带有id的消息(不是接收者的消息ID),它只会发送一个'new'消息。



我怎么能用Gmail API发送回复?



预先感谢您。

正如文档所述,如果您要发送回复并希望请确保:


  1. Subject 标题匹配 li>
  2. 引用和 In-Reply-To 头文件遵循RFC 2822标准。

如果你想自己做,你可以得到 Subject 引用 Message-ID - 您要回复的邮件的标题:



要求:

  userId = me 
id = 14fd1c555a1352b7 //我想要回复的消息的ID。
格式=元数据
metadataHeaders =主题,引用,消息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,
重要,
UNREAD
] ,
snippet:下一级哥们2015-09-15 18:10 GMT + 02:00 Emil Tholin& lt; emtholin@gmail.com& gt;:2015-09-15 18: ,
historyId:575289,
internalDate:1442333414000,
payload:{
mimeType:multipart / alternative,
headers:[
{
name:In-Reply-To,
value:},
{
name:References,
value:},
{
name:Message-ID,//这对于两个用户来说都是一样的,正如你所问的那样。
值:},
{
name:Subject,
value:Re:Cool
}
]
},
sizeEstimate:1890
}

要遵循RFC 2822标准我们已经添加了 Message-ID 我们要回复的消息给引用 -header,用空间。 In-Reply-To -header也具有我们想要回复的消息的值。我们还向 Subject -header添加 Re:,以表明它是一个响应。

  // Base64对邮件进行编码并使其成为URL安全的
//(用 - 替换+,替换/ ,_,删除结尾的=)
var encodedResponse = btoa(
Content-Type:text / plain; charset = \UTF-8 \\\\
+
MIME-Version:1.0 \ n+
Content-Transfer-Encoding:7bit \ n+
参考文献:< CADsZLRxZDUGn4Frx80qe2_bE5H5bQhgcqGk=GwFN9gs7Z_8oZw@mail.gmail.com>gt ;< CADsZLRyzVPLRQuTthGSHKMCXL7Ora1jNW7h0jvoNgR+hU59BYg@mail.gmail.com>< CADsZLRwQWzLB-uq4_4G2E64NX9G6grn0cEeO0L=avY7ajzuAFg@mail.gmail.com> \\\
+
的所有回复地址:其中CADsZLRwQWzLB-uq4_4G2E64NX9G6grn0cEeO0L = avY7ajzuAFg @ mail.gmail.com> \\\
+
Subject:Re:Cool \\\
+
From:sender@gmail.com\\\
+
To :reciever@gmail.com\\\
\\\
+

这是响应文本将去的地方
).replace(/ \\ + /克, - )代替(/ \ //克,_)代替(/ = + $ /,)。;

$ .ajax({
url:https://www.googleapis.com/gmail/v1/users/me/messages/send?access_token=<USER_ACCESS_TOKEN>,
方法:POST,
contentType:application / json,
data:JSON.stringify({
raw:encodedResponse
})
});

正如您所看到的,这对于手动操作而言是一种痛苦。你也可以只是回应线程。然而,这可能不足以满足您的使用情况。

这样,您只需提供邮件和 threadId ,并确保 Subject 是一样的,Google会正确显示它。

  // Base64 - 编码邮件并使其成为URL安全的
//(用 - 替换+,用_替换/,删除尾随的=)
var encodedResponse = btoa(
Content-Type:text / plain; charset = \UTF-8 \\\\
+
MIME-Version:1.0\\\
+
Content -Transfer-Encoding:7bit \\\
+
主题:原始邮件主题\\\
+
发件人:sender@gmail.com\\\
+
收件人:reciever@gmail.com\\\
\\\
+

这是响应文本将出现的位置
).replace(/ \ + / g,' - ').replace(/ \ // g,'_')。replace(/ = + $ /,'');

$ .ajax({
url:https://www.googleapis.com/gmail/v1/users/me/messages/send?access_token=<USER_ACCESS_TOKEN>,
方法:POST,
contentType:application / json,
data:JSON.stringify({
raw:encodedResponse,
threadId:< THREAD_ID_OF_MESSAGE_TO_RESPOND_TO>
})
});


I have two gmail accounts I created a thread consisting of five messages and retreived them with gmail gapi at this page https://developers.google.com/gmail/api/v1/reference/users/threads/get.

This is what I got:

As you can see, the ids don't match, although they identify the exact same letters. Why this happens, and how can i get the unified id?

P.S. The real reason I am doing this is that I need to send a reply to a message with gmail API, but to do that, you need to know the id of the message that you reply to. And if I reply to the message with id that I have ( not the message id that the receiver has ), it just sends a 'new' message.

How can I send replies with Gmail API?

Thank you in advance.

解决方案

As the docs say, if you're trying to send a reply and want the email to thread, make sure that:

  1. The Subject headers match
  2. The References and In-Reply-To headers follow the RFC 2822 standard.

If you want to do this yourself, you could get the Subject, References and Message-ID-headers of the message you want to respond to:

Request:

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

Response:

{
 "id": "14fd1c555a1352b7",
 "threadId": "14fd1c52911f0f64",
 "labelIds": [
  "SENT",
  "INBOX",
  "IMPORTANT",
  "UNREAD"
 ],
 "snippet": "Next level dude 2015-09-15 18:10 GMT+02:00 Emil Tholin &lt;emtholin@gmail.com&gt;: 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
}

To follow the RFC 2822 standard we have added the Message-ID of the message we want to respond to to the References-header, separated with a space. The In-Reply-To-header also has the value of message we want to respond to. We also add Re: to our Subject-header to indicate that it is a response.

// 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
  })
});

As you can see, this is a pain in the backside to to manually. You could also just respond to the thread. This might not be enough for your use case however.

This way, you just have to supply the mail and the threadId, and make sure the Subject is the same, and Google will display it for you correctly.

// 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>"
  })
});

这篇关于如何使用Gmail API发送回复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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