Science and technology

Handle your Gmail filters from the Linux command line

Automation is a scorching matter proper now. In my day job as an SRE a part of my remit is to automate as many repeating duties as doable. But how many people do this in our each day, not-work, lives? This yr, I’m targeted on automating away the toil in order that we are able to concentrate on the issues which can be vital.

Server-side mail guidelines are one of the environment friendly methods to pre-sort and filter mail. Sadly, Gmail, the preferred mail service on the planet, would not use any of the usual protocols to permit customers to handle their guidelines. Adding, enhancing, or eradicating a single rule generally is a time-consuming job within the internet interface, relying on what number of guidelines the person has in place. The choices for enhancing them “out of band” as offered by the corporate are restricted to an XML export and import.

I’ve 109 mail filters, so I do know what a chore it may be to handle them utilizing the offered strategies. At least till I found gmailctl, the command-line instrument for managing Gmail filters with a (comparatively) easy standards-based configuration file.

$ gmailctl take a look at
$ gmailctl diff
Filters:
--- Current
+++ TO BE APPLIED
@@ -1 +1,6 @@
+* Criteria:
+ from: @opensource.com
+ Actions:
+ mark as vital
+ by no means mark as spam

$ gmailctl apply
You are going to use the next adjustments to your settings:
Filters:
--- Current
+++ TO BE APPLIED
@@ -1 +1,6 @@
+* Criteria:
+ from: @opensource.com
+ Actions:
+ mark as vital
+ by no means mark as spam
Do you wish to apply them? [y/N]:

To outline guidelines in a versatile method gmailctl makes use of the jsonnet templating language. Using gmailctl additionally permits the person to export the prevailing guidelines for modification.

To get began, set up gmailctl through your system’s package deal supervisor, or set up from supply with go set up github.com/mbrt/gmailctl/cmd/gmailctl@newest. Follow that with gmailctl init which can stroll you thru the method of organising your credentials and the proper permissions in Google. If you have already got guidelines in Gmail, I like to recommend working gmailctl obtain subsequent, so as to backup the prevailing guidelines. These shall be saved within the default configuration file ~/.gmailctl/config.jsonnet. Copy that file someplace protected for future reference, or to revive your previous guidelines simply in case!

If you want to begin from a clear slate, or you have no guidelines but, you’ll want to create a brand new, empty ~/.gmailctl/config.jsonnet file. The most simple construction for this file is:

native lib = import 'gmailctl.libsonnet';
{
  model: "v1alpha3",
  creator: {
    identify: "OSDC User",
    e mail: "[email protected]"
  },
  guidelines: [
    {
      filter: {
        or: [
          { from: "@opensource.com" },
        ]
      },
      actions: {
        markRead: false,
        markSpam: false,
        markImportant: true
      },
    },
  ]
}

As you’ll be able to see, this file format is much like, however not as strict as JSON. This file units up a easy rule to mark any mail from opensource.com as vital, depart it unread, and never mark it as spam. It does this by defining the standards within the filters part, after which the principles to use within the actions part. Actions embrace the next boolean instructions: markRead, markSpam,markImportant, and archive. You may also use actions to specify a class for the mail, and assign folders, which we’ll get to later within the article.

Once the file is saved, the configuration file format might be verified with gmailctl take a look at. If all the things is sweet, then you need to use gmailctl diff to view what adjustments are going to be made, and gmailctl apply to add your new rule to Gmail.

$ gmailctl diff
Filters:
---
Current
+++ TO BE APPLIED
@@ -1,6 +1,8 @@
* Criteria:
from: @opensource.com Actions:
+ archive
  mark as vital
  by no means mark as spam
+ apply label: 1-Projects/2022-OSDC

$ gmailctl apply -y
You are going to use the next adjustments to your settings:
Filters:
--- Current
+++ TO BE APPLIED
@@ -1,6 +1,8 @@
* Criteria:
  from: @opensource.com Actions:
+ archive
  mark as vital
  by no means mark as spam
  apply label: 1-Projects/2022-OSDC

Applying the adjustments...

As talked about beforehand, new mail messages might be auto-filed by setting labels within the configuration. I wish to assign all mails from Opensource.com to a folder particularly for them, and take away them from the inbox (or archive in Gmail phrases). To do this, I’d change the actions part to be:

  actions: {
        markRead: false,
        markSpam: false,
        markImportant: true,
        archive: true,
        labels: [
          "1-Projects/2022-OSDC"
        ]
      },

As you’ll be able to see within the picture above, gmailctl diff now exhibits solely what will change. To apply it, I used gmailctl apply -y to skip the affirmation immediate. If the label would not exist, then an error is given, since a filter can’t be made for a label that doesn’t exist already.

You may also make extra advanced guidelines that focus on particular situations or a number of emails. For instance, the next rule makes use of an and situation to search for messages from Cloudflare that aren’t buy confirmations.

 filter: {
        and: [
          { from: "[email protected]" },
          { subject: "[cloudflare]" },
          { question: "-{Purchase Confirmation}" }
        ]
      },

In the case of a rule that performs the identical motion on a number of messages, you need to use an or construction. I exploit that to file all emails referring to tabletop video games to a single folder.

 filter: {
        or: [
          { from: "[email protected]" },
          { from: "[email protected]" },
          { from: "[email protected]" },
          { from: "[email protected]" },
          { from: "[email protected]" },
          { from: "@elventower.com" },
          { from: "[email protected]"},
          { from: "[email protected]" },
          { from: "[email protected]" },
          { from: "@monkeyblooddesign.co.uk" },
        ]
      },

For folks with a number of Gmail accounts that want their very own units of guidelines, you’ll be able to specify a novel configuration file for them with the --config command line parameter. For instance, my work makes use of Gmail, and I’ve an entire different algorithm for that. I can create a brand new gmailctl listing, and use that for the work configuration, like so:

$ gmailctl --config ~/.gmailctl-work/ diff

To make this simpler on myself, I’ve two shell aliases to make it clear which configuration I’m utilizing.

alias gmailctl-home="gmailctl --config $HOME/.gmailctl"
alias gmailctl-work="gmailctl --config $HOME/.gmailctl-work"

The one disadvantage gmailctl has is that it’ll not apply a brand new filter to present messages, so you continue to need to manually do issues for mail obtained earlier than doing gmailctl apply. I hope they can type that out sooner or later. Other than that, gmailctl has allowed me to make including and updating Gmail filters quick and virtually utterly computerized, and I can use my favourite e mail shopper with out having to continually return to the online UI to vary or replace a filter.

Most Popular

To Top