【问题标题】:Google scripts Bounced emailsGoogle 脚本 退回的电子邮件
【发布时间】: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


    【解决方案1】:

    “以编程方式”调用代码的一种方法是:

    function RunReport(){
      findBouncedEmails_();
    }
    

    无论如何,问题中的代码很好,但它需要使用新的 Google Apps 脚本运行时 V8,因为它使用了

    1. 箭头函数
    2. 模板字符串
    3. letconst

    【讨论】:

    • 感谢指正。但是,如何更改此箭头功能。应搜索并弄清楚。如果你能给出一些指示,这将有助于改变这三个。
    • 在问题的代码中尝试用function ()替换() =&gt;
    【解决方案2】:

    您在函数“RunReport()”内声明函数“findBouncedEmails_”。

    还有……

    等等……

    你永远不会调用它。 (根据您提供的代码)。


    解决办法:

    如果您要“以编程方式调用”的函数名为“RunReport”,那么一种方法是编写以下语句:

    findBouncedEmails_();

    在函数声明之后。

    注意:

    不必在RunReport 中声明findBouncedEmails_


    更新:

    正如@Rubén 指出的那样,编辑器接受为有效的语法取决于脚本的运行时间,您可以在incompatibilities 中看到语法的不兼容性。

    【讨论】:

    • 更新了我的答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-04
    • 2015-03-10
    • 2013-11-01
    • 2010-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多