如何判断通过Gmail REST API发送的电子邮件是否已弹出? [英] How to tell if an email sent via Gmail REST API has bounced?

查看:164
本文介绍了如何判断通过Gmail REST API发送的电子邮件是否已弹出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过Gmail API发送电子邮件,并希望知道邮件何时反弹。我怎么做到这一点?



据我所知,反弹的电子邮件通常包含某种表示反弹的标题,例如:

  X-Failed-Recipients:zzzzzzzzzasasdfasdfadfa@gmail.com 

然而,似乎并不总是有一个标题,表明它是什么原始消息ID被反弹。



我正在考虑以下计划,但在那里有很多漏洞,我认为我必须接近这个错误。


  1. 通过Gmail API发送电子邮件(发送至失败的电子邮件)--- >成功完成

  2. 接收电子邮件退回的电子邮件收件箱

  3. 扫描包含退回邮件的电子邮件的电子邮件

  4. 试图找出哪些原始邮件被退回。

问题


  • Gmail api会返回Gmail邮件ID,而不是实际的邮件ID

  • 必须持续监视/轮询收件箱以查看是否存在反弹电子邮件

  • 甚至有可能通过存在标题进行搜索?

  • 每个电子邮件提供商似乎都有不同的反弹标题

  • 标题可能不会显示原始消息ID



我还有其他一些想法:

>


  • 在主题中搜索包含字符串Undeliverable的电子邮件

  • 不使用gmail rest api发送因为反弹跟踪不可行。也许使用SMTP API呢?


解决方案

当通过Gmail API发送时,来自邮件程序deamon( mailer-daemon@googlemail.com )的响应。您可以不断检查用户的消息,以查看是否收到来自守护进程的新消息。



确保存储自上次检查以秒为单位的时间戳,以便您

  query = from:mailer-daemon@googlemail.com after:< TIME_SINCE_EPOCH_IN_SECONDS> 

GET https://www.googleapis.com/gmail/v1/users/me/messages?q=from%3Amailer-daemon%40googlemail.com+after%3A1437055051&access_token={YOUR_API_KEY}

回应:

  {
messages:[
{
id:14e97f7ed03b7e88,
threadId:14e97f7ea9b794a4
},
]
}

我得到了反弹!让我们抓取整个邮件并对其进行解码并获取您所指定的Message-ID。

  GET https:// www。 googleapis.com/gmail/v1/users/me/messages/14e97f7ed03b7e88?fields=payload%2Fbody%2Fdata&access_token={YOUR_API_KEY} 

回应:

  {
payload: {
body:{
data:RGVsA0K ...
}
}
}

将邮件从其URL安全版本转换为常规base64(将所有 - 替换为+和_替换为/),以及base64解码它:

  atob(RGVsA0K ...。replace(/ \- / g,' +')。replace(/ \_ / g,'/')); 

解码的邮件:

 永久失败的递送失败:

sadsadsadas@sadsads.asdsad

永久失败的技术细节:
DNS错误:sadsads.asdsad的地址解析失败:未找到域名

-----原始消息-----





已收到:从292824132082.apps.googleusercontent.com以
命名为unknown未与HTTPREST共享gmailapi.google.com;星期四,2015年7月16日13:44: 43 -0400
from:example@gmail.com
日期:2015年7月16日星期四13:44:43 -0400
消息ID:< this_is_it@mail.gmail.com>
主题:主题文本
发送至:sadsadsadas@sadsads.asdsad
Content-Type:text / plain; charset = UTF-8

实际的消息文本放在此处

在这里,我们有Message-ID!让我们得到反弹的电子邮件!

  query = rfc822msgid:< this_is_it@mail.gmail.com>gt ;; 

GET https://www.googleapis.com/gmail/v1/users/me/messages?q=rfc822msgid%3A%3CCADsZLRzOs1wT4B5pgR7oHHdbjkQhuaCQQs8CEckhLwVw73QFEQ%40mail.gmail.com%3E&key={YOUR_API_KEY}

回应:

  {
messages:[
{
id:14e97f7ea9b794a4,//< - 这是反弹!
threadId:14e97f7ea9b794a4
}
],
}


I'm sending emails via Gmail API and would like to know when the messages bounce. How can I do this?

As I understand it, bounced emails usually contain some sort of header indicating a bounce such as:

X-Failed-Recipients: zzzzzzzzzasdfasdfadfa@gmail.com

However, there doesn't seem to always be a header indicating what original messageID it was that bounced.

I was thinking of the following plan, but there are so many holes that I think I must be approaching this wrong.

  1. Send email (to a failed email) via Gmail API ---> It goes through successfully
  2. Receive the email bounced email inbox
  3. Scan email for emails containing bounced headers
  4. Try to figure out which original email it was that bounced.

Problems

  • Gmail api returns Gmail Message ID, not the actual message ID
  • Have to continuously monitor/poll inbox to see if there's bounced emails
  • Is it even possible to search by existence of a header?
  • Each email provider out there seems to have different bounce headers
  • The headers might not indicate original Message ID

A couple other ideas I had were to:

  • Search for emails with the string "Undeliverable" in the subject?
  • not use gmail rest api for sending as bounce tracking is not feasible. perhaps use SMTP api instead?

解决方案

Messages that gets bounced when sent through the Gmail API gets a response from the mailer deamon (mailer-daemon@googlemail.com). You could continually check the user's messages to see if a new message from the daemon has been received.

Make sure to store the timestamp in seconds since your last check, so you don't get any nasty duplicates next time around.

query = from:mailer-daemon@googlemail.com after:<TIME_SINCE_EPOCH_IN_SECONDS>

GET https://www.googleapis.com/gmail/v1/users/me/messages?q=from%3Amailer-daemon%40googlemail.com+after%3A1437055051&access_token={YOUR_API_KEY}

Response:

{
 "messages": [
  {
   "id": "14e97f7ed03b7e88",
   "threadId": "14e97f7ea9b794a4"
  },
 ]
}

I got a bounce! Let's fetch the entire mail and decode it and get the Message-ID you were alluding too.

GET https://www.googleapis.com/gmail/v1/users/me/messages/14e97f7ed03b7e88?fields=payload%2Fbody%2Fdata&access_token={YOUR_API_KEY}

Response:

{
 "payload": {
  "body": {
   "data": "RGVsA0K..."
  }
 }
}

Converting the mail to regular base64 from its URL safe version (replace all "-" with "+" and "_" with "/"), and base64-decoding it we get:

atob("RGVsA0K...".replace(/\-/g, '+').replace(/\_/g, '/'));

Decoded mail:

"Delivery to the following recipient failed permanently:

     sadsadsadas@sadsads.asdsad

Technical details of permanent failure: 
DNS Error: Address resolution of sadsads.asdsad. failed: Domain name not found

----- Original message -----

.
.
.

Received: from 292824132082.apps.googleusercontent.com named unknown by
 gmailapi.google.com with HTTPREST; Thu, 16 Jul 2015 13:44:43 -0400
from: example@gmail.com
Date: Thu, 16 Jul 2015 13:44:43 -0400
Message-ID: <this_is_it@mail.gmail.com>
Subject: Subject Text
To: sadsadsadas@sadsads.asdsad
Content-Type: text/plain; charset=UTF-8

The actual message text goes here

Here we have the Message-ID! Let's get the bounced email!

query = rfc822msgid:<this_is_it@mail.gmail.com>;

GET https://www.googleapis.com/gmail/v1/users/me/messages?q=rfc822msgid%3A%3CCADsZLRzOs1wT4B5pgR7oHHdbjkQhuaCQQs8CEckhLwVw73QFEQ%40mail.gmail.com%3E&key={YOUR_API_KEY}

Response:

{
 "messages": [
  {
   "id": "14e97f7ea9b794a4", // <-- Here is the message that bounced!
   "threadId": "14e97f7ea9b794a4"
  }
 ],
}

这篇关于如何判断通过Gmail REST API发送的电子邮件是否已弹出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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