This document explains how to use filters in the Gmail API.
You can use the
settings.filters
resource to configure advanced filtering rules for an account. Filters can
automatically add or remove labels or
forward emails to verified
aliases based on the
attributes or content of the incoming message.
For information on how to
create,
list,
get, or
delete
Gmail filters, see the
settings.filters
resource.
Match criteria
Filter messages by properties such as the sender, subject, date, size, and message contents. Filters only apply to specific messages and not the entire email thread. Any query using Gmail's advanced search syntax can also be used in a filter. For example, common filter patterns include:
| Filter | Matches |
|---|---|
criteria.from='sender@example.com' |
Emails from sender@example.com |
criteria.size=10485760criteria.sizeComparison='larger' |
Emails larger than 10 MB |
criteria.hasAttachment=true |
Emails with an attachment |
criteria.subject='[People with Pets]' |
Emails with [People with Pets] in the subject |
criteria.query='"my important project"' |
Emails containing my important project |
criteria.negatedQuery='"secret knock"' |
Emails not containing secret knock |
If multiple criteria are present in a filter, a message must satisfy all criteria for the filter to apply.
Actions
Apply an
Action
to messages matching the filter criteria. With an Action, you can forward
messages to a verified email address, or add and remove
labels.
Add or remove labels to change the disposition of the email. For example, some common actions include:
| Action | Effect |
|---|---|
action.removeLabelIds=['INBOX'] |
Archive the email (skip the inbox) |
action.removeLabelIds=['UNREAD'] |
Mark as read |
action.removeLabelIds=['SPAM'] |
Never mark as spam |
action.removeLabelIds=['IMPORTANT'] |
Never mark as important |
action.addLabelIds=['IMPORTANT'] |
Mark as important |
action.addLabelIds=['TRASH'] |
Delete the email |
action.addLabelIds=['STARRED'] |
Mark as starred |
action.addLabelIds=['<user label id>'] |
Tag the mail with a user-defined label. Only one user-defined label is allowed per filter. |
Code samples
The following code samples show how to label and archive messages from a mailing list: