Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Script to label emails if it contains specific words in the subject or body on Gmail

Update: Thank you so much endothermic_dragon! You are brilliant! It all works now.

I have created scripts before but never for Gmail. I am having a hard time creating a script to filter emails coming in and label and archive them if they have specific words. We receive about 100 emails daily from various senders relating to a group of companies. Each company has its label. Usually the name of the company is in the subject or body of the email. I am trying to build a script that gets each email, check if the array with subjects or the array with plainBody contains the name of one or more of the labels (each label is the name of a company) in the labels array from getUserLabels(), and if so, assign the respective label(s) to it. I have tried everything, from filter() to forEach() to indexOf() but nothing seems to work. I feel the arrays from getUserLabels() and getInoboxThreads() are 2D, and I'm struggling to work with them. I've rewritten the code multiple times and it got me nowhere. I really hope one of you can shed some light on this.

Example: Let's suppose we work with Apple, Amazon and Netflix. Each of these companies have their own label. If an email comes in and has "Apple" in the subject or body, then add label "Apple" to it and archive it. If it has "Amazon" and "Netflix" in the subject or body, then add labels Amazon and Netflix.

Final code:

function labelEmails() {
  var labels=[]
  GmailApp.getUserLabels().forEach(function(label){labels.push(label.getName())})
  var threads = GmailApp.search('is:unread').forEach(function(email){
    email.getMessages().forEach(function(thread){
      //Filter - https://developers.google.com/apps-script/reference/gmail/gmail-message
      //Example used - if body contains 'hi', mark as read.
      labels.forEach(function(label){
        if (thread.getBody().toLowerCase().includes(label.toLowerCase())){
          labelToAssign = GmailApp.getUserLabelByName(label);
          labelToAssign.addToThread(email)
        } else if (thread.getSubject().toLowerCase().includes(label.toLowerCase())) { 
           labelToAssign = GmailApp.getUserLabelByName(label);
          labelToAssign.addToThread(email)
          } 
      })
    })
    });
}

Some of our labels are abbreviations of the name of the companies, so I also needed a function to handle those. You can check it out below too: (Companies in the code are random companies, not the real ones)

function labelExceptions() {
  var exceptions = [{exc:'apple', labeln: 'APPL'}, {exc: 'amazon', labeln: 'AMZ'}, {exc: 'Bank of America', labeln: 'BOFA'}]
  var threads = GmailApp.search('is:unread').forEach(function (thread){
    thread.getMessages().forEach(function(message){
      exceptions.forEach(function(exception){
        if (message.getBody().toLowerCase().includes(exception.exc)){
          labeltoAssign = GmailApp.getUserLabelByName(exception.labeln);
          labeltoAssign.addToThread(thread)
        } else if (message.getSubject().toLowerCase().includes(exception.exc)){
          labeltoAssign = GmailApp.getUserLabelByName(exception.labeln);
          labeltoAssign.addToThread(thread)
        }
      
      })
    
    })}
 );
  } 

Thank you very much!

like image 533
Victor Avatar asked Nov 19 '25 23:11

Victor


1 Answers

Checked with my gmail, works!

function myFunction() {
  var labels=[]
  GmailApp.getUserLabels().forEach(function(label){labels.push(label.getName())})
  var threads = GmailApp.search('is:unread').forEach(function(email){
    email.getMessages().forEach(function(thread){
      //Filter - https://developers.google.com/apps-script/reference/gmail/gmail-message
      //Example used - if body contains 'hi', mark as read.
      labels.forEach(function(label){
        if (thread.getBody().toLowerCase().includes(label.toLowerCase())){
          email.markRead()
        }
      })
    })
    });
}
like image 196
Endothermic_Dragon Avatar answered Nov 22 '25 11:11

Endothermic_Dragon