google-apps-scriptgmailgmail-api

Google apps script forwarding pdf's sorted by label sends emails to two labels at the same time


I run a company that gets a lot of invoices. There are a couple of email adresses that these invoices are sent to. All email addresses lead to one Gmail inbox. The emails are then tagged based on the specific emailaddress they are sent to.

In this gmail I run a script with google apps script that checks the email for the label, checks if there is a pdf or XML file, if so it sends a new email with the attachment to an email address where the invoice is automatically processed to be payed. It then removes the label so it will not be processed twice.

Every label has its own script that runs on a timer. Here's the script of one of the labels:

function sendAttachmentsToEmailWGF() {
  var threads = GmailApp.search('label:westergas');
  
  for (var i = 0; i < threads.length; i++) {
    var messages = threads[i].getMessages();
    
    for (var j = 0; j < messages.length; j++) {
      var attachments = messages[j].getAttachments();
      var rightAttachments = [];
      var subject = messages[j].getSubject();
      var body = messages[j].getBody();
      
      for (var k = 0; k < attachments.length; k++) {
        var attachment = attachments[k];
        
        if (attachment.getContentType() === "application/pdf") {
          rightAttachments.push(attachment);
        } else if (attachment.getContentType() === "text/xml") {
          rightAttachments.push(attachment);
        } else if (attachment.getContentType() === "application/octet-stream") {
          rightAttachments.push(attachment);
        }
      }
      
      if (rightAttachments.length > 0) {
        var newEmailBody = "Original Email Subject: " + subject + "\n\n" + body;
          var bijlage = rightAttachments;
          MailApp.sendEmail('ottenenkampschreur.10543@mailtobasecone.com', subject, newEmailBody, {attachments: bijlage});
          
        }
        
        }
        
        threads[i].removeLabel(GmailApp.getUserLabelByName('westergas'));
        threads[i].addLabel(GmailApp.getUserLabelByName('westergas_sent'));
      }
    }

All this is to prevent inline images or other files to reach our accounting software because that will process random images as new invoices an clogs up the flow.

Now for the most part it all works very well. I only have a problem with a couple of very specific suppliers. They send separate emails to two email adresses but almost at the same time and with a exactly the same message. The only difference is the attachment (different number) and the address there are sent to. Gmail then tags these two emails both with two labels, one for each email address. The are separate emails but they are somehow stacked and both tagged with two labels. The script then sends both attachments to both email adresses. That means that label 'A' gets attachments 'a' and 'b'. and label 'B' also gets attachments 'a' and 'b'. It means that both invoices get payed twice!

Long story short: How do I stop gmail from bunching these two emails up, that are sent to two different emails inside one inbox.

I have already turned 'conversation mode' off in settings. I tested what happened if I manually tagged one of these emais with only one label and then run the scripts. It still gets sent twice to both receiving emails!


Solution

  • In the end I fixed this by making a new separate gmail inbox and taking one of the invoice adressess out of the other inbox. Running the scripts in both accounts.

    I must have something to do with using one gmail inbox and getting messages that are too similar. Gmail will somehow group them together even tough the receiving email adresses are different and the attachments are also different. I thought turning off conversation mode will fix this bit it did not. Anyway I will leave this all here if someone ever has a similar problem.