Search code examples
google-apps-scriptgmail

Create a new filter in Gmail using Google Apps Script


I want to create a new label and a new filter in Gmail using Google Apps Script which will push all new emails in a particular label. I have already created a label which is working fine but I am unable to create a filter.

I know it's easy to create a new filter in the search criteria of GMail. But I want to automate exactly the same feature and then publish it to my team of 100 people so they don't have to do it manually.

Any help on this is highly appreciated.

Many Thanks, Prachi


Solution

  • update 2016: the new gmail api can create filters. Currently you cannot create gmail filters using apps script but it can be done without filters.
    Using apps script, add a label then archive (moveToArchive). that is a "move" out of the inbox and into the label.

    A gmail filter is better because it does the "apply label and archive" real-time while the apps script can run at most once per minute using a time trigger.