且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

发送电子邮件时使用gmail API调用脚本

更新时间:2022-11-17 07:51:30

可能有几种方法可以实现此目的,但是我使用的步骤如下:

There are probably several ways to achieve this, but the steps I use myself are the following:

在SENT文件夹中列出消息,并仅要求提供最多1个结果的ID(将为您提供最近发送的消息):

GET https://www.googleapis.com/gmail/v1/users/me/messages?labelIds=SENT&fields=messages%2Fid&maxResults=1&key={YOUR_API_KEY}

回复:

{
 "messages": [
  {
   "id": "1234"
  }
 ]
}

获取代表该邮件发送时间的historyId:

GET https://www.googleapis.com/gmail/v1/users/me/messages/14e6525456e7c793?fields=historyId&key={YOUR_API_KEY}

回复:

{
 "historyId": "123456"
}

每隔一定的时间查看历史记录,只需查看SENT标签下添加的邮件,然后在后续请求中使用新的historyId(如果响应中有一个):

GET https://www.googleapis.com/gmail/v1/users/me/history?labelId=SENT&fields=history%2FmessagesAdded%2ChistoryId&startHistoryId=500446&key={YOUR_API_KEY}

回复:

{
 "history": [
  {
   "messagesAdded": [
    {
     "message": {
      "id": "135674567",
      "threadId": "2342456432",
      "labelIds": [
       "SENT",
       "INBOX",
       "UNREAD",
       "IMPORTANT"
      ]
     }
    }
   ]
  }
 ],
 "historyId": "12233445" //Use this in subsequent request!
}