Calling a script with gmail api when an email is sent

会有一股神秘感。 提交于 2019-12-13 01:01:33

问题


We have a sales team that uses gmail to send emails to their customers. We would like to be able to log those emails to our internal system and I wondered if there is anything in the gmail api that would allow for some script to be called when an email is sent? If so, is there any sample code for this functionality?


回答1:


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

List the messages in the SENT-folder and ask for just the id with a maximum of 1 result (will give you the most recently sent):

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

Response:

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

Get the historyId that represents the point in time this mail was sent:

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

Response:

{
 "historyId": "123456"
}

Look at the history at a certain interval, just looking at added messages under the SENT-label, and use the new historyId in your subsequent requests if there is one in the response:

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

Response:

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


来源:https://stackoverflow.com/questions/31254744/calling-a-script-with-gmail-api-when-an-email-is-sent

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!