It doesn't look like it supports filtering on headers -- at least I don't see any calls to GmailMessage.getHeader(), and the only call to GmailMessage.getRawContent() appears to be in retrieving the ListID header (which Gmail's search natively supports). Looks like there's an open feature request for generalized header matching: https://github.com/ranmocy/gmail-automata/issues/36
I noticed that in the article I referenced (https://mjasion.pl/label-gitlab-notifications/) they also don't mention using getHeader and instead use getRawContent. It makes me wonder if the getHeader API was added more recently than this article and the project you mention.
I wrote a simple scripting utility to do similar labeling actions, bacause I came to gsuite with a large procmail configuration file previously. It was useful for a while, but gradually I stopped updating the filters:
I built a Google Apps Script Gmail add-on (with a UI and card view for a selected email) for archiving email contents as a PDF to an internal enterprise document system using GAS and AWS API Gateway. Here are two extremely annoying problems that I really can't fix using this platform:
1. Calls to UrlFetchApp.fetchAll() will fail with an "Unexpected Error" exception about every 3 to 8 percent of calls. The way my code works is that I'll make one GET request to an AWS API Gateway endpoint to request a pre-signed S3 URL, and then I'll use the pre-signed S3 url to PUT a document. The unauthorized first request will fail sometimes, and issuing a PUT to the pre-signed S3 url will fail sometimes too. I have found no reason that these requests should fail, so instead of torturing myself by trying to figure out the root cause, I've decided to wrap both fetchAll() calls in a loop that retries the exact same request three times before giving up. If there was anything wrong with the request itself, one would expect a retry loop to be pointless, but it's significantly reduced the number of add-on failures.
2. If an email has invalid SPF alignment, then the add-on won't run at all. It's been three years and there's still no workaround for this. I have had to explain to dozens of people over the past three years that there's nothing I can do about it. https://issuetracker.google.com/issues/112064778 .
I feel like Google Apps Script - especially Gmail add-ons - are sorely neglected and are treated as an annoying legacy product that will probably be discontinued very soon with no replacement.
Yes, it's incredibly annoying how little attention it seems to get from google. Bugs sit in the bug tracker for years and while I like the redesign it broke a function I used (to get the current URL so create multi-page apps). Apps Script just got a new UX update not long ago which I took as a good sign but App Maker had a roadmap going out a year when it got killed, so.
Indeed, at my workplace we use Gmail and it's been a lifesaver for sorting email. Now of course it would not be necessary if only Google implemented sieve or at least proper header and substring search in the backend filters; but some functionality such as tagging and untagging entire threads is very useful.
I also use Apps Script at home to send me email if the home network goes offline (a script at home updates a cell in a worksheet every 10 minutes, one on the cloud checks that it's been updated recently). It has saved my frozen goods at least once.
More interesting than GmailApp, DriveApp, etc. are the Gmail, Drive etc. classes. They're under the "advanced" services, and far more powerful (and faster). Highly recommend them for more serious uses of Google Apps Script.
- For more real-time filtering you might also want to look into integrating with Google's Pub/Sub service (see Gmail.newWatchRequest() and Gmail.Users.watch()). It's kind of a pain to set up but it can help you avoid having to wait 10 minutes for everything to run, though you'll still want to have a polling mechanism as a fallback, and you might need to periodically call the watch methods to ensure you keep getting notifications pushed to you.
- If you need even better performance you can try manually batching up the HTTP requests via googleapis.com/batch (you can use ScriptApp.getOAuthToken() to get the auth token). This will let you factor out some of the server overhead across messages.
Are you referring to actual Gmail filters, or Google Apps Scripts that attempt to mimic Gmail filters? I don't recall seeing the former be flaky, but the latter is another story.
The majority of the spam came in with emojis in the from or subject line, so this was immediately helpful for that, then I just add a rule whenever I notice certain phrases popping up more often.
I’d be delighted if someone can tell me I’m wrong, but it seems like Gmail filters don’t have built in options for stuff like emojis or regular expressions. Very disappointing that I have to brute force script my own filters.
Beware that even "serious" companies are now starting to include emojis in the subject line. I had a flight cancellation notification email come up with plane emojis in the title... I almost overlooked it as some kind of quasi-spam marketing email (the titles was something like "[plane emojis] about your flight [plane emojis]"). Perhaps it was the intention since it included information about EU regulated refunds.
21 comments
[ 2.5 ms ] story [ 72.3 ms ] threadIt doesn't look like it supports filtering on headers -- at least I don't see any calls to GmailMessage.getHeader(), and the only call to GmailMessage.getRawContent() appears to be in retrieving the ListID header (which Gmail's search natively supports). Looks like there's an open feature request for generalized header matching: https://github.com/ranmocy/gmail-automata/issues/36
I noticed that in the article I referenced (https://mjasion.pl/label-gitlab-notifications/) they also don't mention using getHeader and instead use getRawContent. It makes me wonder if the getHeader API was added more recently than this article and the project you mention.
I wrote a simple scripting utility to do similar labeling actions, bacause I came to gsuite with a large procmail configuration file previously. It was useful for a while, but gradually I stopped updating the filters:
https://github.com/skx/labeller
Nice to see there are other people doing similar things though, I'll definitely take a look at this more carefully over the next few days.
1. Calls to UrlFetchApp.fetchAll() will fail with an "Unexpected Error" exception about every 3 to 8 percent of calls. The way my code works is that I'll make one GET request to an AWS API Gateway endpoint to request a pre-signed S3 URL, and then I'll use the pre-signed S3 url to PUT a document. The unauthorized first request will fail sometimes, and issuing a PUT to the pre-signed S3 url will fail sometimes too. I have found no reason that these requests should fail, so instead of torturing myself by trying to figure out the root cause, I've decided to wrap both fetchAll() calls in a loop that retries the exact same request three times before giving up. If there was anything wrong with the request itself, one would expect a retry loop to be pointless, but it's significantly reduced the number of add-on failures.
2. If an email has invalid SPF alignment, then the add-on won't run at all. It's been three years and there's still no workaround for this. I have had to explain to dozens of people over the past three years that there's nothing I can do about it. https://issuetracker.google.com/issues/112064778 .
I feel like Google Apps Script - especially Gmail add-ons - are sorely neglected and are treated as an annoying legacy product that will probably be discontinued very soon with no replacement.
I also use Apps Script at home to send me email if the home network goes offline (a script at home updates a cell in a worksheet every 10 minutes, one on the cloud checks that it's been updated recently). It has saved my frozen goods at least once.
- For more real-time filtering you might also want to look into integrating with Google's Pub/Sub service (see Gmail.newWatchRequest() and Gmail.Users.watch()). It's kind of a pain to set up but it can help you avoid having to wait 10 minutes for everything to run, though you'll still want to have a polling mechanism as a fallback, and you might need to periodically call the watch methods to ensure you keep getting notifications pushed to you.
- If you need even better performance you can try manually batching up the HTTP requests via googleapis.com/batch (you can use ScriptApp.getOAuthToken() to get the auth token). This will let you factor out some of the server overhead across messages.
Next step in my things-to-try list is to just download the inbox via imap and sort it out locally.
Are you referring to actual Gmail filters, or Google Apps Scripts that attempt to mimic Gmail filters? I don't recall seeing the former be flaky, but the latter is another story.
That leads to polling, which ends up with a lackluster user experience. I don't want a random 30 second delay added to all my mail delivery.
The majority of the spam came in with emojis in the from or subject line, so this was immediately helpful for that, then I just add a rule whenever I notice certain phrases popping up more often.
I’d be delighted if someone can tell me I’m wrong, but it seems like Gmail filters don’t have built in options for stuff like emojis or regular expressions. Very disappointing that I have to brute force script my own filters.