Calling a script with gmail api when an email is s

2019-08-12 07:56发布

问题:

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!
}


标签: gmail-api