101

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?

67

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

  • Not very intuitive, but yes. I think most to: should be deliveredto: actually, to support BCCs. – lapo Apr 2 '12 at 16:42
  • Has this worked with wildcards too, for example, using a catch-all email address in deliveredto:*@mydomain.com? I am trying it but will take time to test. – Marcos Apr 24 '12 at 12:08
  • 1
    Note that you can use the search bar at the top of GMail to construct your advanced search, then click the arrow at the end of the search bar and select "Create filter with this search." – Feckmore Apr 3 '13 at 21:17
  • 2
    This does not solve the problem for headers other than Delivered-To? (Say a search for the Auto-submitted or Precedence headers) – Gert van den Berg Sep 14 '16 at 10:46
  • 2
    It's pretty obnoxious that the header text is "Delivered-To:" while the "has" field eliminates the '-' to yield "deliveredto:". – James Bowery Sep 7 '18 at 17:05
35

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]

  • Thanks. Even their pages make it difficult to determine if a NOT operator - would work on wildcard addresses, eg: -*@mydomain.com in the To: field – Marcos Apr 24 '12 at 12:10
31
  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)
  }
}
  • 11
    My unproven gut reaction is that google will have something in place to stop users looping through gigs worth of raw email content every minute. So I'd advise to replace " var threads = GmailApp.getInboxThreads();" with " var threads = GmailApp.search("newer_than:1h");" – Rick Jul 31 '15 at 21:06
  • I am using var MyEmails = GmailApp.search("is:unread in:inbox"); in order to search all email in the inbox which are not yet read – Ronen Ariely Aug 14 '18 at 15:09
  • @Rick - I wrote a script that uses the IMAP interface to walk through all 13+GB in my gmail account. 'Wouldn't surprise me if script.google.com allowed it. – broofa Aug 23 '18 at 14:09
0

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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.