Setting up a header-based automatic labeller for Gmail
  Last updated:
  
    
The purpose of this guide is to set up a new hourly-executed App Script for:
- Iterating through all emails in your Inbox; and
- Archive and label each thread that has at least one email with a custom header.
Requirements
- A label in Gmail, that you want to attach to this script
Setup
I. Creating a new App Script
- Access the App Script dashboard.
- Press the "New project" button on the left.
- Click the name of the project, and set it to "AutoLabeller".
- Paste the below code snippet:
- Change the configuration of the script:- SEARCHED_HEADER: SMTP header to be searched. For example,- X-ACME-Shared-Email: true; and
- LABEL: Gmail label to be attached to the emails. For example, the nested label- shared-inboxes/acme.
 
- Add a new service, choose Gmail API, and click "Add".
const MAX_AGE = "1h"
const SEARCHED_HEADER = ""
const LABEL = ""
function processInbox() {
    var label = GmailApp.getUserLabelByName(LABEL)
    var threads = GmailApp.search("label:inbox newer_than:" + MAX_AGE)
    for (var i = 0; i < threads.length; i++) {
        var thread = threads[i]
        if (hasHeader(thread)){
            thread.addLabel(label)
            thread.moveToArchive()
            console.log("Detected email to label, with ID " + thread.getId())
        }
    }
}
function hasHeader(thread) {
    var messages = thread.getMessages()
    for (var j = 0; j < messages.length; j++) {
        var message = messages[j]
        var body = message.getRawContent()
        if (body.indexOf(SEARCHED_HEADER) > -1) {
            return true
        }
    }
    return false
}
II. Creating a Trigger
- Select the clock icon from the left menu.
- Press the "Add Trigger" button.
- Choose the following details:- Function: processInbox;
- Time-based trigger: "Hour timer"; and
- Interval: "Every hour".
 
- Function: 
- Save the timer.
III. Deploying the Script
- Press the "Deploy" button from the top menu, and choose "New Deployment".
- Choose the "Web app" type, and change the description to "Script for labelling email by inspecting the headers".
- Press the "Deploy" button.