Gmail with Google Script Automation

Teepob Harutaipree
Bright Days
Published in
5 min readJan 14, 2020

Ever had a messy inbox full of mails? Does it bother you to skip the inbox?

Luckily, Google had provided many services that we can harness its utilities to organize the mail using personal preferences or workflow. You will able to make an auto email archiver/remover after following the steps. This will be done by using Google script and setting the schedule trigger. The code is pretty simple and straight forward since Google had already provided us ready-for-use objects for managing Gmail.

3 simple steps ✅

  • Setting up New Project
  • Create the Script
  • Schedule the Trigger

⭐️ If you have just started out to code this is a great opportunity to learn.

I also cover — how to debug the code for beginners.

🗼Create a New Project

First go to this website https://script.google.com, log in with your google accounts.

Click the Button: New Project

䷔ Create the Script

Start with the simple script below.

This script will archive an email from the sender “no-reply@youtube.com” every time the code is executed (10 mail max).

Let’s look at first line the variable which is the search operators that will filter the specific mail.

To Illustrate look at the picture below to see if the search operator work

For more operators: https://support.google.com/mail/answer/7190?hl=en
searchThread = GmailApp.search(search, 0, 10)

Google Script has GmailApp Instance available for use and one of the methods that we can use is search that will take 3 parameters:

  • search operators — “no-replay@youtube.com”
  • start index number — 0 (no skipping)
  • number of maximum threads/mail to be obtained — 10 mail max
GmailApp.moveThreadsToArchive(searchThread)

As the name suggested: moveThreadsToArhive , move threads/mails obtained from earlier step to Archive.

Another Simple Example:

This script will remove mails that were opened and older than 5 days.

This script will remove mails that were opened and older than 5 days.

👟Test your Script

Try Run or Debug your code for the first time, you will get the popup.

To run or debug the scripts, first, you need to grant this scripts access to your Gmail.

Click Advanced > Hit the Link Go to Your App Name (unsafe) > Click Allow Button

🚨 Set the Trigger for Automation

First, go back to the Google Script home page.

Select the Menu > My Trigger > + Add Trigger
Configure Trigger Options

If you want to run the script every day follow the same configuration as the above picture.

  1. Select the function that you want to run
  2. Set the trigger type weekly/daily/hourly.
  3. Select the time that you want to trigger this function

Hit Save and You should see the trigger

After finished you will see the trigger is set.

Navigate to My Execution, the execution by the trigger will show here as below.

CONGRATULATION — YOU ARE ALL SET!

Bust all those mail with your new invention.

🐞How to Debug — For starters

The benefits from debugging: Understand how scripts behave line by line and what are the actions that your code is executing.

  • Go to the code line by line
  • Inspect the variables
  1. Click at the Line number of when you want to inspect the code (All the code above that line are executed)
  2. Select the drop-down to select the function that you want to run (If the option are not there, try hit CTRL+S to save the script)
  3. Click the bug icon (Debug)

After the debug session has started there are actions that you can take.

At this point in the picture the code freeze at line 5, and there is an inspect window below that you can view current variables value before hitting the code on that line.

🐛There are multiple actions for continuing the debugging session

  1. ▶️ Move the state to next debug point.
  2. ⏹ Stop the debugging/execution
  3. Step In: Step into the function or external call (if no reference Step-Over)
  4. Step Over: move to the next Line.
  5. Step Out: Step out of the function

From the above picture, you can see searchThread variable has the length of 2 so when you resume this code two mail will be moved to Thrash.

This way I know the code works and I can stop the execution if I don’t want to proceed to delete my mail.

😍 Enhancement

These are some useful queries that can be included in the code.

  • Curly braces: {item1 item2} include multiple instances in the category.
  • Dash/Minus — put in front of the filter category to flag as excluding.

For more information on GmailApp Instance and available methods take a look at the link below.

The example I showed is one simple way to organize your Gmail. There are more possible & creative ways that you can manage and control the mail in the Gmail account effortlessly — such as connecting to more modules like drive and sheets.

--

--

Teepob Harutaipree
Bright Days

💻 Full stack Developer who likes to exercise 🏊‍♂️and living the healthy lifestyles. 📑 Exploring and incorporating productivity tools in daily routine.