Is it possible to create a Gmail filter that works on headers other than From, To, Subject?

Loading...

Is it possible to create a Gmail filter that works on headers other than From, To, Subject?

I've forwarded one of my really old email addresses to Gmail.  I'd like to create a filter so that any mail to that old address is always labeled properly in Gmail.
I can create a Gmail filter with the "To: " containing that address, but that doesn't catch all mails to that address, only the ones that are explicitly addressed.  Often, there is spam where somebody else is the explicit recipient, and my old address was simply Bcc'd.  The "To: " filter doesn't work.
What I'd like to do is filter on the "Delivered-To:" header.  Is that possible with Gmail or am I stuck with the three standard options of From, To, Subject?

Solutions/Answers:

Answer 1:

Yes, you can filter by delivered-to. Just create a new filter with deliveredto:youremail@address.com in the has the words field.

Answer 2:

Here is a list of all the advanced operators:

from:
to:
subject:
OR
– (hyphen)
label:
has:attachment
list:
filename:
” ” (quotes)
( )
in:anywhere
in:inbox
in:trash
in:spam
is:important
is:starred
is:unread
is:read
has:yellow-star
has:red-star
has:orange-star
has:green-star
has:blue-star
has:purple-star
has:red-bang
has:orange-guillemet
has:yellow-bang
has:green-check
has:blue-info
has:purple-question
cc:
bcc:
after:
before:
older:
newer:
older_than:
newer_than:
is:chat
deliveredto:
circle:
has:circle
category:
size:
larger:
smaller:
+ (plus sign)
rfc822msgid:
has:userlabels
has:nouserlabels

[source]

Answer 3:

  1. go to https://script.google.com and create a new Google Apps Script

new google apps script.

  1. select the “Create script for Gmail” option

    • this will create a script project
    • this will create a script called Code.gs
    • this will create sample functions, one them named processInbox
  2. save the project

  3. customize the script and test it until you are satisfied
  4. find the “custom project’s triggers” button

    custom trigger button

    • this will let you run the script on a timer (e.g. daily, or every minute)

Example:

processInbox

This function will iterate over all messages in the Inbox.

function processInbox() {
   // process all recent threads in the Inbox (see comment to this answer)
   var threads = GmailApp.search("newer_than:1h");
   for (var i = 0; i < threads.length; i++) {
      // get all messages in a given thread
      var messages = threads[i].getMessages();
      for (var j = 0; j < messages.length; j++) {
         var message = messages[j];
         processMessage(message);
      }
   }
}

Go one step further and call GmailApp.search() if you want to do something fancy instead of GmailApp.getInboxThreads().

processMessage

This function will take care of processing a single message; you get the raw content with getRawContent() and try to find your header in there, if it is in there, tag the message.

function processMessage(message) {
  var body = message.getRawContent();
  if (body.indexOf("X-Custom-Header: some-custom-value") > -1) {
    // do stuff with message (e.g. add label)
  }
}

Answer 4:

You can use the deliveredto: operator in the Has the words field when creating a filter, as @mvime indicated. However, not all email providers append Delivered-to to the email header, so a more reliable way is to forward you@oldemail.com to you+oldemail@gmail.com, and filter by deliveredto:(you+oldemail@gmail.com).

Our Awesome Tools

References

Loading...