GMail Scripts: Putting an automatic label on new e-mails

The last couple of posts have been tech related due to heavy work and/or lack in activity on other interesting topics I like to yap about. Football season is over and summer seems to be finally setting in (although the current weather has it’s own ideas) . Anyway, I’ve been going over some JavaScript ideas and I ran over regular expression the other day. I thought it might be a good idea to incorporate regular expression into something a bit more useful.

A couple of months back, while I was learning JavaScript apps, I came upon Google scripts. It seemed powerful and useful but my limited JavaScript knowledge didn’t allow me to do anything useful with it. A month or so later, I found out that I was getting lots of emails that are work related and didn’t want to miss/delete any by mistake. Thankfully, GMail offers ‘labels’ which is just a simple way of marking your emails so you can easily differentiate them. I, manually, labelled my work emails for a couple of days until I realized how pointless it was. If I can mark them manually, then it becomes meaningless to use labels in the first place. And if I miss any, it would be even harder to notice the mistake. Then I remembered Google script and wondered if I could use it to automatically label all incoming emails.

For some background, Google script (actual name: Google Apps Script) is based on JavaScript and as a result, it is very easy to learn. It employs most syntax used in JavaScript, although there are differences to keep in mind. It’s very useful in doing personal projects and I would recommend it to anyone currently learning JavaScript. A huge benefit of using this is knowing that you can schedule your scripts to run at specific times due to being hosted on Google Drive. My script runs once every hour but that can be changed to even once every minute.

I usually receive my work emails from a web-app ticketing system. This usually leads to the emails subject having specific keywords that are common in every work-related email. Hence, I can easily employ Regular Expressions to search for these keywords and apply labels to any new email that contains them. As long as the keywords are unique for work-related emails and all work-emails contain them, I won’t risk losing any mis-labelled emails. The script can be found here. You might need to be logged into google to view it. You’ll also need to set up a ‘script trigger’ which is essentially a way of allowing google to know when and how often to run the script.

I pasted the script below, in case it isn’t visible from the above link:

var label = GmailApp.getUserLabelByName(“MY LABEL NAME”); //getting a label name you’ve specified
var firstThread = new Array(); //An Array that will contain your emails
var matching = /keyword/g; //the keyword you’ll be searching
function lab (){
 for (var i = 0; i<20; i++){ //you can set whatever number depending on the volume of emails you receive on average
  firstThread.push(GmailApp.getInboxThreads(0,50)[i]); //getting all the inbox messages
  var testing = firstThread[i].getFirstMessageSubject().match(matching); //checking whether the 'keyword' in the subject
   if (testing == "keyword"){ //checks whether testing returns match
   label.addToThread(GmailApp.getInboxThreads(0,50)[i])//adds the label to the message thread
   testing = null; //emptying testing before searching the next mail
  }
 }
}

//Couple of useful things to point out:
//- I search for threads because it's easier to look for keywords and label the whole thread instead of labelling individual emails
//- Ensure that the keywords are unique enough to not label emails that aren't related

Advertisements

One thought on “GMail Scripts: Putting an automatic label on new e-mails

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s