【发布时间】:2020-11-24 15:19:57
【问题描述】:
以下 stackoverflow 链接 (Google Scripts - Grab email address from bounced message and parse information) 提供了有关如何使用来自 Amit Agarwal 的脚本 (https://www.labnol.org/internet/gmail-bounced-email-report/29209/) 处理反弹的非常好的信息。
我的问题是是否可以通过编程方式调用代码?我尝试将每个代码 sn-ps 放入函数并调用它们,但徒劳无功。
function RunReport() {
const findBouncedEmails_ = () => {
try {
const rows = [];
const {
messages = []
} = Gmail.Users.Messages.list("me", {
q: "from:mailer-daemon",
maxResults: 200
});
if (messages.length === 0) {
toast_("No bounced emails found in your mailbox!");
return;
}
toast_("Working..");
for (let m = 0; m < messages.length; m += 1) {
const bounceData = parseMessage_(messages[m].id);
if (bounceData) {
rows.push(bounceData);
}
if (rows.length % 10 === 0) {
toast_(`${rows.length} bounced emails found so far..`);
}
}
writeBouncedEmails_(rows);
} catch (error) {
toast_(error.toString());
}
};
}
我也尝试将代码复制到我的项目中,带有“const”的行显示语法错误,无法将代码保存在工作表中。
在https://stackoverflow.com/users/1595451/rubén 的输入生成如下代码后,它将收集退回电子邮件的日期、主题和电子邮件,并将数据存储在 google 表格中。发布此内容以帮助他人。
function getBouncedEmails() {
// Create a sheet in your project "BouncedEmails" and add this code in the script.
var sheet = SpreadsheetApp.getActive().getSheetByName('BouncedEmails');
// Fetch all the bounced emails using a query
var query = "from:(mailer-daemon@google.com OR mailer-daemon@googlemail.com)";
//Get the most recent 500 bounced email messages
//Change the number according to your requirement
GmailApp.search(query, 0, 500).forEach(function(thread) {
thread.getMessages().forEach(function(message) {
if (message.getFrom().indexOf("mailer-daemon") !== -1) {
//get the emailAddress from the Header
var emailAddress = message.getHeader('X-Failed-Recipients');
//add a filter if you would like to write certain messages only
if(thread.getFirstMessageSubject() == "YOUR SUBJECT"){
// Save the data in Google Spreadsheet
sheet.appendRow([
thread.getLastMessageDate(),
thread.getFirstMessageSubject(),
emailAddress]);
}
}
});
});
}
首次运行时,您需要提供访问相应 Gmail 邮箱的权限。在编码方面一切顺利。
【问题讨论】:
标签: javascript google-apps-script google-sheets