Gmail – Match archive/read status of duplicate e-mails

gmailgmail-filters

Unfortunately, I tend to get a lot of duplicate e-mails: same sender, same subject, same content. Gmail recognizes the duplication, and minimizes these e-mails when I view them, so I’d like to tap into that recognition.

Specifically, I would like a filter that archives an e-mail, if it is a duplicate of an e-mail already archived, and to mark the e-mail as read, if it is a duplicate of an e-mail already read.

Is there any way to do this?

Best Answer

Here is a script that may help with the issue. It takes the sender and subject of the 1st message in every inbox thread, and runs a search for them. There isn't a search parameter for the body of the message, so instead every candidate message is compared to the inbox message. If a match is found, its state is determined and the inbox message is treated accordingly.

The script editor has an option (under Resources) to install a time-based trigger that executes the script periodically: every day, every hour, every minute, or at some other intervals. (There is no "new email" event-based trigger.)

Note: I haven't tested the script, since I don't have such duplicates.

Reference: GmailApp documentation and pages linked from there.

function checkDupes() {
  var threads = GmailApp.getInboxThreads();
  for (var i = 0; i < threads.length; i++) {
    var message = threads[i].getMessages()[0];
    var sender = message.getFrom();
    var subject = message.getSubject();
    var body = message.getPlainBody();
    var candidates = GmailApp.search('from:'+sender+' subject:'+subject);
    var found = false; 
    for (var j = 0; j < candidates.length; j++) {
      var messages = candidates[i].getMessages();
      for (var k = 0; k < messages.length; k++) {
        if (messages[k].getPlainBody() === body) {
          found = true;
          if (!messages[k].isUnread()) {
            message.markRead();
          }
          if (!messages[k].isInInbox()) {
            GmailApp.moveThreadToArchive(threads[i]);
          }
          break;
        }
      }
      if (found) {
        break;
      }
    }
  }
}