67

I need to create a Google App Script to respond to machine generated emails, except those also sent to one colleague or contain a particular topic outside my responsibility.

I've been trying to use the GmailApp.search API which includes all mail from user X but NOT those emails which are also addressed to user Y NOR those containing "junk_term". I want the emails archived and to skip the inbox.

I created a query like so:

 "from:(user_X@address.com) -{user_Y@address.com OR junk_term}"

But it appears not to return any results.

steinybot
  • 5,491
  • 6
  • 37
  • 55
pterandon
  • 1,676
  • 1
  • 13
  • 15
  • Folks, this really doesn't work for me, and I need this functionality to help my work. How to make a better question? – pterandon Aug 25 '15 at 20:46
  • Have you tried only "-{user_Y@address.com OR junk_term}" and see if this grammar worked? – chenghuayang Aug 26 '15 at 05:11
  • Okay. That helps. "userY@address.com" by itself is responded to intelligently by the filter. If you try to do any boolean logic with terms like "from:userY@address.com", the gmail filter process bastardizes it into a non-working search entry. but you set me on right path. – pterandon Aug 26 '15 at 12:54

2 Answers2

70

Fixing this problem requires understanding two things:

1) If a label has been placed on an existing email by a filter, the label stays even if that filter were deleted. One must delete the label itself, and then re-create a new filter in order to get your edits to "take".

2) There is a bug in the Gmail filter system. If you try to set up complex Boolean such as

from:user1@me.com -to:user2@me.com -junk_term

It will work fine on the initial search, but if you use it to create a filter, the terms, especially the NOTs, will be garbled.

The correct syntax, (hat tip to chenghuayang) is to ignore the to's and from's.

user1@me.com -user2@me.com -junk_term
Community
  • 1
  • 1
pterandon
  • 1,676
  • 1
  • 13
  • 15
  • 2
    That does a full-text search and will also match FWD's or RE's were `user1@me.com` appears in the body. Just keep that in mind. – CarHa Aug 27 '15 at 00:15
  • 2
    Right, but the NOT to: option is broken due to a bug – pterandon Aug 27 '15 at 01:02
  • 1
    Note that you may need to go into your Gmail Settings and briefly set "Conversation view off" while verifying that your searches are working, as the conversation view on (where it groups replies in the same "email chain") may cause some slightly unexpected search results to either be or not be filtered out. – Gabriel Staples Jan 16 '19 at 19:14
6

You could use labels to flag your emails and process them accordingly, based on those labels:

  1. matches from: user_X@address.com then apply label Label_X
  2. matches from: user_Y@address.com then apply label Label_Y
  3. matches junk_term then apply label Label_Y
  4. matches label:label_X AND -label:Label_Y then respond

The condition of the last rule has to be added to the Includes the words field.

Here is a list of further attributes.

CarHa
  • 1,148
  • 11
  • 31
  • 1
    That's part of the problem. That technique gives error: 'Filter searches containing "label:", "in:", "is:", or stars criteria (i.e. "has:yellow-star") are not recommended as they will never match incoming mail. Do you still wish to continue to the next step?' – pterandon Aug 26 '15 at 12:47
  • 1
    It's not an error message, but it asks for a 'confirmation'. ...and don't treat recommendations as a dogma! – CarHa Aug 27 '15 at 06:26
  • 1
    The [filters in gmail are not applied in any order, they will be applied randomly](https://support.google.com/mail/thread/4111485?hl=en). So this won't work. – Adze Oct 13 '20 at 09:13