通过 Gmail API 发送时被退回的邮件会从邮件程序守护程序 (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, '/'));
解码邮件:
"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
这里有 Message-ID!让我们收到退回的电子邮件!
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}
回应:
{
"messages": [
{
"id": "14e97f7ea9b794a4", // <-- Here is the message that bounced!
"threadId": "14e97f7ea9b794a4"
}
],
}