如何防止Gmail线程回来 [英] How to keep gmail threads from coming back

查看:66
本文介绍了如何防止Gmail线程回来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个功能,我每小时运行一次.它检查收件箱中是否有邮件列表.然后,将其重新格式化,保存附件,并将其转发到另一个电子邮件地址,然后表面上丢弃已处理的消息.效果很好,除了....

I have this function, which I run hourly. It checks the inbox for messages that are to a mailing list. It then reformats them a bit, saves attachments, and forwards them along to another email address, and then ostensibly throws away the processed messages. Works pretty well, except....

我最近遇到了一个不会死的线程.它已收到50多个回复,并且当新的回复以相同的主题行进入时,线程中所有已处理的旧消息都将再次处理.因此,我的发件箱和收件人的电子邮件收件箱都呈指数增长.我不确定是否只是简单地不有效地删除已发送的消息,或者是否有其他方法可以识别线程中已处理的消息,以便在将更多消息添加到线程中时不会再次处理它们.我不知道这是应用程序脚本问题还是Gmail问题.

I have recently encountered a thread that just won't die. It's got well over 50 replies, and when new replies come in with the same subject line all of the old messages in the thread that have already been processed, get processed again. So my sent box grows exponentially, as does the recipient email inbox. I'm not sure if I'm simply not deleting the sent messages effectively, or if there is some other way I should be identifying already processed messages within a thread so they don't get processed again when more messages are added to the thread. I can't tell if this is an app script problem or a gmail problem.

这是我的代码:

function processPMFMail(){
  var threads = GmailApp.getInboxThreads();
  var message = GmailApp.getMessagesForThreads(threads); 
  for(var i = 0; i < message.length; i++) {
    for(var j = 0; j < message[i].length; j++) {
      var from = message[i][j].getFrom();
      var to = message[i][j].getTo();
      var cc = message[i][j].getCc();
      var bcc = message[i][j].getBcc();
      var fields = {to, cc, bcc};
                    //Logger.log(to);

                    if (
                    [to,cc,bcc].some(field =>
                    ["EMAIL-1","EMAIL-2"].some(email => field.includes(email))
                    )
                    ) {
                    var subject = cleanSubject(message[i][j]);
                    if (subject == "DoNotPost") {
                    Logger.log("Email not for posting - " + cleanSubject(message[i][j]));
                    message[i][j].moveToTrash();
                   } else{
                    var from = parseEmailName(message[i][j]);
                    var body = message[i][j].getPlainBody();
                    var hBody = message[i][j].getBody();
                    var attachment = saveAttachments(message[i][j]);
                    var opening = "<br>From: " + from + "<br> Sent: "+ Utilities.formatDate(message[i][j].getDate(), "US/Eastern", "M/d/yy @ h:mm aaa") + "<br><br>" + attachment;
                    if (hBody.indexOf("</body>")>-1) {
                    hBody = removeForwards (hBody);
                    hBody = hBody.replace("</body>",opening + "</body>").trim();
                   } else{
                    hBody = hBody + opening;
                   }
                    Logger.log(subject + opening);
                    //send the message
                    GmailApp.sendEmail("EMAIL-3", subject , opening + body , 
                    {htmlBody: hBody, 
                    name: from
                   })  
    }  // end of processed messages
    //move the message to the trash
    message[i][j].moveToTrash();
  } 
  else {Logger.log("No Messages to forward")}
}
}

}

非常感谢任何指针.

-DH

推荐答案

因此,如果我正确理解,您将一遍又一遍地从线程中获取所有消息.

So If I understood correctly you are getting all the message from a thread over and over.

但是最终这是可以预期的,因为您首先获得了 GmailThread ,然后检查每个 GmailMessage 在线程中.即使您已经检查并删除它,它也是该线程的一部分,使用

But in the end this is expected as you are getting first the GmailThread and then checking every GmailMessage in the thread. Even if you have already checked it up and deleted it is part of the Thread and is normal that you get it back when using getMessagesForThreads().

我建议使用 isInTrash() 在每封邮件中(在for嵌套循环中),以检查是否已将此邮件移至垃圾箱.

I would recommend using isInTrash() in every message (inside the for nested loop), to check if you have already moved this message to the trash.

类似这样的东西:

function processPMFMail() {
    var threads = GmailApp.getInboxThreads();
    var message = GmailApp.getMessagesForThreads(threads);
    for (var i = 0; i < message.length; i++) {
        for (var j = 0; j < message[i].length; j++) {
            if (!message[i][j].isInTrash()) {
                var from = message[i][j].getFrom();
                var to = message[i][j].getTo();
                var cc = message[i][j].getCc();
                var bcc = message[i][j].getBcc();
                var from = message[i][j].getFrom();
                var fields = { to, cc, bcc };
                //Logger.log(to);

                if (
                    [to, cc, bcc].some(field =>
                        ["EMAIL-1", "EMAIL-2"].some(email => field.includes(email))
                    )
                ) {
                    var subject = cleanSubject(message[i][j]);
                    if (subject == "DoNotPost") {
                        Logger.log("Email not for posting - " + cleanSubject(message[i][j]));
                        message[i][j].moveToTrash();
                    } else {
                        var from = parseEmailName(message[i][j]);
                        var body = message[i][j].getPlainBody();
                        var hBody = message[i][j].getBody();
                        var attachment = saveAttachments(message[i][j]);
                        var opening = "<br>From: " + from + "<br> Sent: " + Utilities.formatDate(message[i][j].getDate(), "US/Eastern", "M/d/yy @ h:mm aaa") + "<br><br>" + attachment;
                        if (hBody.indexOf("</body>") > -1) {
                            hBody = removeForwards(hBody);
                            hBody = hBody.replace("</body>", opening + "</body>").trim();
                        } else {
                            hBody = hBody + opening;
                        }
                        Logger.log(subject + opening);
                        //send the message
                        GmailApp.sendEmail("EMAIL-3", subject, opening + body,
                            {
                                htmlBody: hBody,
                                name: from
                            })
                    }  // end of processed messages
                    //move the message to the trash
                    message[i][j].moveToTrash();
                }
                else { Logger.log("No Messages to forward") }
            }
        }
    }

}

这篇关于如何防止Gmail线程回来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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