Web Applications Stack Exchange is a question and answer site for power users of web applications. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
Not being able to filter by any arbitrary header has been a pain for me for quite a long time. That's why I've decided to develop FilterMyGmail, an extension that will address this and other related issues. – jcollado Jan 25 '14 at 23:13

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

share|improve this answer
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
It seems to work for me. I never thought it wouldn't, so I haven't tested it exhaustively, but I haven't noticed any missing messages in the 3+ years that I've been using such filters. – mvime Apr 24 '12 at 17:17
Marcos, I replied to your question you posted. Try adding quotes around the "@domain.com" in your search. – JonathanMumm Apr 25 '12 at 2:13
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

Here is a list of all the advanced operators:

share|improve this answer
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
  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)



This function will iterate over all messages in the Inbox.

function processInbox() {
   // process all threads in the Inbox
   var threads = GmailApp.getInboxThreads();
   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];

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


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)
share|improve this answer
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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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