Get all emails from Gmail with a specific label, using Google App Scripts
Posted on | May 15, 2014 | 1 Comment
Introduction
Something I use at work is a task-tracking website. At the basic level, you give the task a short title, a long description, one or more flags, and estimate how long it will take to complete. A cool feature of this website is that they offer an api for registered users.
The boss wants any IT support issues to be logged into this task-tracking website so that any of the support team can see what has or has not been started and fix the issue.
All issues for IT support are being sent to a single mailing list on Gmail. This, I thought, is a perfect opportunity to play with Google Apps Scripts.
In this blog post I am going to show how to get your recent emails from Gmail, and POST to an API depending on the email’s label.
Google Apps Scripts
Google Apps Script is a scripting language based on JavaScript that lets you do new and cool things with Google Apps like Docs, Sheets, and Forms, and your scripts run on Google's servers.
The steps below show how to get into the scripts editor, ready for you to make your first script:
- Visit script.google.com to open the script editor. (You’ll need to be signed in to your Google account.) If this is the first time you’ve been to script.google.com, you’ll be redirected to a page that introduces Apps Script. Click Start Scripting to proceed to the script editor.
- A welcome screen will ask what kind of script you want to create. Click Blank Project or Close.
- Delete any code in the script editor.
Final Code
Here is the final code. It’s well documented so should be easy enough to read through, but I’ll walk through it all at the bottom because I enjoy the sound of my own voice :)
/** * Retrieves all inbox threads. If IT support email, then create * a story in Tracker. * For more information on using the GMail API, see * https://developers.google.com/apps-script/reference/gmail/ */ function processInbox() { var newLabel = GmailApp.getUserLabelByName("In Tracker"), oldLabel = GmailApp.getUserLabelByName("For Tracker"), threads = oldLabel.getThreads(), url = 'https://task-tracker.com/api/new/'; // get all threads with old label for (var i = 0; i < threads.length; i++) { // switch labels threads[i].addLabel(newLabel).removeLabel(oldLabel).refresh(); //get message details var msg = threads[i].getMessages(), subject = msg[0].getSubject(), body = msg[0].getPlainBody(); // POST to Tracker var options = { "method" : "post", "payload" : { 'title':subject, 'description':body, 'flags':{"IT Support"} } }; UrlFetchApp.fetch(url, options); } };
The name of the function does not matter. When we tell Google to run the script it will give us a list of declared function names, so it really is just for your benefit to give it a descriptive name.
We declare our global variables – two (previously created) labels (set up labels in Gmail), all threads/emails with the label we want to add to our task tracker, and finally we specify the API url we are going to post to.
The script now loops through all of the retrieved threads (thread is an email with 0 or more replies / messages), and swaps out the existing label for a different label (so we don’t keep adding this one email), and we get the message details ready for posting to the API.
Finally, we do a POST to the url using the global object ‘UrlFetchApp’.
Running the script
At the moment this script will not do anything. We need it to be checking our email on a fairly regular basis. We could have it run every five minutes, or even every single minute! Yeah, let’s do that one!
From the menu bar, select Resources and click on “Current project’s triggers”.
The first time you load this screen you will get a pop-up from Google saying “This app needs authorisation to run.”. This is because the script is attempting to access your emails. This is neccessary for the script to work, so accept the app’s permissions.
Click on the ‘No triggers set up. Click here to add one now.’ button. Change the ‘Events’ to ‘Time-driven’, and the other tabs to ‘Minutes timer’ and ‘Every minute’, then click save.
Conclusion
This script is now set to check our emails every minute, looking for any emails with our pre-set labels, before changing the labels and doing a POST request to our API with the email message details as our data. Cool beans.
Happy Coding.
Posted By:Richard Parnaby-King