157

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?

3

4 Answers 4

98

Yes, you can filter by "Delivered-To". Just create a new filter with deliveredto:[email protected] in the has the words field.

🛈 Note that the "has" filter name doesn't have "-" in its name as opposed to the original email header name.

14
  • 6
    Not very intuitive, but yes. I think most to: should be deliveredto: actually, to support BCCs.
    – lapo
    Commented Apr 2, 2012 at 16:42
  • 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
    Commented Apr 3, 2013 at 21:17
  • 4
    This does not solve the problem for headers other than Delivered-To? (Say a search for the Auto-submitted or Precedence headers) Commented Sep 14, 2016 at 10:46
  • 9
    It's pretty obnoxious that the header text is "Delivered-To:" while the "has" field eliminates the '-' to yield "deliveredto:". Commented Sep 7, 2018 at 17:05
  • 3
    This does not work anymore, I tried a few variants too. Commented Sep 26, 2021 at 11:11
58
  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)
  }
}
7
  • 15
    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
    Commented Jul 31, 2015 at 21:06
  • 1
    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 Commented Aug 14, 2018 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
    Commented Aug 23, 2018 at 14:09
  • 5
    For the benefit of anyone coming to this like I was trying to do exactly this, you can't add a label to a message, only to the thread, so you'll need to do message.getThread().addLabel(GMailApp.getUserLabelByName("Some Label"))
    – Dave_J
    Commented Jul 10, 2020 at 10:58
  • 2
    Very impressive! But why message.getRawContent() and body.indexOf("X-Custom-Header: some-custom-value") and not just message.getHeader() and body == "X-Custom-Header: some-custom-value"? If agreed, please modify the answer.
    – LWC
    Commented Jun 11, 2021 at 19:45
53

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]

2
  • 1
    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
    Commented Apr 24, 2012 at 12:10
  • Google gives me Filter searches containing "label:", "in:", "is:", date range (e.g. "before:" or "after:") or stars criteria (e.g. "has:yellow-star") are not recommended as they will never match incoming emails. Do you still wish to continue to the next step? - is is: actually a viable filter in some way? Commented Nov 6, 2021 at 5:44
6

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 [email protected] to [email protected], and filter by deliveredto:([email protected]).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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