发送电子邮件时使用gmail API调用脚本 [英] Calling a script with gmail api when an email is sent

查看:98
本文介绍了发送电子邮件时使用gmail API调用脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个使用gmail向其客户发送电子邮件的销售团队.我们希望能够将这些电子邮件记录到我们的内部系统中,我想知道gmail api中是否有什么可以在发送电子邮件时调用某些脚本?如果是,是否有此功能的示例代码?

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?

推荐答案

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

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

这篇关于发送电子邮件时使用gmail API调用脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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