如何使用Gmail API发送回复

时间:2015-09-15 15:05:18

标签: javascript gmail gmail-api gapi

我有两个gmail帐户,我创建了一个由五条消息组成的线程,并在此页https://developers.google.com/gmail/api/v1/reference/users/threads/get使用gmail gapi进行了检索。

这就是我得到的:

enter image description here

正如您所看到的,ids不匹配,尽管它们识别完全相同的字母。为什么会发生这种情况,如何获得统一ID?

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

如何使用Gmail API发送回复?

提前谢谢。

2 个答案:

答案 0 :(得分:20)

作为docs say,如果您尝试发送回复并希望电子邮件发送,请确保:

  1. Subject标题匹配
  2. ReferencesIn-Reply-To标头符合RFC 2822标准。
  3. 如果您想自己这样做,可以获得要回复的邮件的SubjectReferencesMessage-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", // 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 - 标头中,并用空格分隔。 In-Reply-To - 标头也具有我们想要响应的消息的值。我们还将Re:添加到我们的Subject - 标头中,以表明它是一个回复。

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

答案 1 :(得分:0)

@Tholle的回答(谢谢!)是正确的,这使我走上了正确的轨道,但是经过最近的更改之后:

https://gsuiteupdates.googleblog.com/2019/03/threading-changes-in-gmail-conversation-view.html

我不得不混淆他的两条道路。

在我的程序中,我必须回复一个线程,但是如果我仅包含threadId(作为#2),则新消息仅由Gmail放入回复者的邮件中,而Gmail则将其放入原始邮件中发件人(也叫gmail)显示为新主题。

我通过在最后一条消息的ID中同时包含threadId和标头“ References”和“ In-Reply-To”来解决。