问题
Good Morning,
I am a newbie to javascript, google scripts, and regex. I have found very good information from Amit Agarwal at https://www.labnol.org/internet/gmail-bounced-email-report/29209/, but I'm having difficulty identifying what I specifically need to keep or need to update. Rather than using the bounce mailer-daemon addresses, I would like to grab the bounced messages from two labels in my gmail account (MinervaBounce and MILSBounce) and be able to differentiate which one the message came from. Any help would be greatly appreciated.
Here are the items that are needed from the bounce message:
- Email date
- Label name
- Bounce email address
- Bounce reason
- Library name from message body
- Bounce Message
Here is an example of the bounce message text:
The original message was received at Tue, 31 Jul 2018 07:16:38 -0400 from localhost [127.0.0.1]
----- The following addresses had permanent fatal errors ----- (reason: 554 5.7.1 < margolulubelle@gwi.net >: Recipient address rejected: user margolulubelle@gwi.net does not exist)
----- Transcript of session follows ----- ... while talking to mx.gwi.net.cust.b.hostedemail.com.:
DATA <<< 554 5.7.1 < margolulubelle@gwi.net >: Recipient address rejected: user margolulubelle@gwi.net does not exist 554 5.0.0 Service unavailable <<< 554 5.5.1 Error: no valid recipients
---------- Forwarded message ---------- From: library.noreply@library.org To: margolulubelle@gwi.net Cc: Bcc: Date: Tue, 31 Jul 2018 07:16:38 -0400 Subject: Circulation Notice From Your Library
07-31-18 07:16PM
Fremont Memorial Library
555 Fremont Ave
Freedom, ME 04941
Margo Madeup-Name
555 Dartmouth Ln
Dixfield, ME 04224
******************************************************************
LIBRARY REQUEST / HOLD CANCELLED
******************************************************************
YOUR REQUEST FOR THE FOLLOWING MATERIAL COULD NOT BE FILLED, OR THE
HOLD WAS FULFILLED, BUT NOT PICKED UP IN TIME. CONTACT YOUR LIBRARY
TO PURSUE OTHER AVENUES TO OBTAIN OR RE-REQUEST THE ITEM.
AUTHOR: Blue Green Yellow Red CALL NO: FIC BLU BARCODE: 3400599999 Fremont PL Children's Rm
88:1
Here is Amit's code:
/*
* Gmail Bounced Emails
* @labnol December 14, 2016
* Written by Amit Agarwal
* email: amit@labnol.org
* twitter: @labnol
* web: https://ctrlq.org
*/
function onOpen(e) {
SpreadsheetApp.getUi().createMenu("🚀 Gmail")
.addItem("Bounce Report", "b_")
.addItem("Help and Support", "h_")
.addToUi();
h_(false);
}
function b_(){var e=SpreadsheetApp.getActiveSheet();e.getRange(2,1,e.getLastRow(),e.getLastColumn()).clearContent();
var t="in:anywhere from:(mailer-daemon@google.com OR mailer-daemon@googlemail.com)",a='=HYPERLINK("URL";"View")',o=0;GmailApp.search(t,0,500).forEach(function(t){t.getMessages().forEach(function(r){if(r.getFrom().indexOf("mailer-daemon")!==-1){var i=r.getPlainBody(),l=i.match(/Delivery to[\s\S]+?(\S+\@\S+)\s([\s\S]+?)-----/)||i.match(/Your message wasn't delivered to (\S+\@\S+) ([\s\S]+)/);
if(l){var n=l[2].match(/The response.+:\s+(.+)/)||l[2].match(/The error.+:\s+(.+)/)||l[2].match(/Technical details.+:\s+(.+)/)||["",l[2].trim()];o++,e.appendRow([t.getLastMessageDate(),l[1],n[1].replace(/ (Please|Learn|See).*$/,""),a.replace("URL",t.getPermalink()),t.getFirstMessageSubject()])}}}),Utilities.sleep(100)}),0===o&&(o="ZERO"),h_(o)}
function h_(e){var t=HtmlService.createTemplateFromFile("help");t.report=e?"The Google Script found COUNT bounced emails in your mailbox.".replace("COUNT",e):"Please go to the 🚀 Gmail menu and select Bounce Report to get started.";
var a=t.evaluate().setTitle("Bounce Report for Gmail").setWidth(460).setHeight(225);SpreadsheetApp.getActiveSpreadsheet().show(a)}
Thank you, Lynn
回答1:
Just adjust the query to your needs, it may look like this:
from:(mailer-daemon@googlemail.com OR mailer-daemon@google.com) AND (label:"SO Review" OR label:"Sample Wala")
This will get the bounce email from multiple labels (OR
operator is needed to make it match on any label, not requiring that both label is found in an email).
Use Users.messages: list
and the query to get the list of messageID
that matches your query then use threadID
to get the original message before it failed to send. Lastly use Users.messages
to view the details about the original message.
References:
https://developers.google.com/gmail/api/v1/reference/users/messages/list https://webapps.stackexchange.com/questions/10581/filtering-based-on-multiple-labels-in-gmail
来源:https://stackoverflow.com/questions/51616036/google-scripts-grab-email-address-from-bounced-message-and-parse-information