Gmail contact-based filtering
Gmail filter sender not in contacts: route unknown senders safely.
Gmail has no “sender is not in my contacts” filter condition. You can exclude known senders with -from: in a filter's Has the words field, run a free Apps Script that labels anyone missing from Google Contacts, or use KeepKnown, which checks contacts, VIP domains, and people you have replied to as mail arrives.
Last updated
The missing condition is contact membership. Gmail can match a sender you already know, but it cannot ask whether a sender is absent from Google Contacts. Google's current filter criteria cover fields such as sender, recipient, subject, words, size, date, and attachments—not the sender's relationship to you.
That leaves three practical options: list the senders you trust with -from: exclusions, run a small Apps Script that reads Google Contacts, or let KeepKnown evaluate the relationship before routing. KeepKnown recognizes contacts, approved domains, people you have replied to, and sender groups. Everyone else moves to a review label instead of being blocked or deleted.
Short answer
Gmail can't filter by “not in contacts,” but you can work around it.
Gmail filters only match addresses, words, sizes, and dates. To catch senders who are not in your contacts, exclude trusted senders with -from:, run a free Apps Script that checks Google Contacts, or use KeepKnown to route unknown senders to KK:OUTSIDERS automatically.
Free tool
Build the filter instead of typing it
The free filter creator turns From, Subject and keyword fields into a Gmail search you can test, a mailFilters.xml file you can import, and the same rule for Outlook. It runs in your browser and nothing you type is sent anywhere.
Filter mail that is not from your trusted senders (-from:)
- Select Show search options in the Gmail search bar.
- In Has the words, type -from:alice@example.com -from:yourcompany.com. Each -from: entry excludes one address or domain.
- Select Search and check that only unfamiliar senders remain.
- Select Create filter, choose Apply the label (for example Not in contacts), and add Skip the Inbox if you want them out of the inbox.
- Edit the filter whenever you start writing to someone new.
Free Apps Script: label senders not in Google Contacts
This script labels inbox mail from anyone who is not in your Google Contacts or Other contacts. It leaves the mail in the inbox; set ARCHIVE to true to archive it as well.
// Screen Gmail senders who are not in your Google Contacts.
// Runs in your own Google account. Nothing is sent to KeepKnown.
const LABEL_NAME = 'Not in contacts';
const ARCHIVE = false; // true: label and archive. false: label only.
const SEARCH = 'in:inbox newer_than:1d';
function screenInbox() {
const known = knownAddresses_();
const label = GmailApp.getUserLabelByName(LABEL_NAME) || GmailApp.createLabel(LABEL_NAME);
GmailApp.search(SEARCH, 0, 100).forEach(function (thread) {
const labelled = thread.getLabels().some(function (existing) {
return existing.getName() === LABEL_NAME;
});
const fromKnownSender = thread.getMessages().some(function (message) {
return known.has(addressOf_(message.getFrom()));
});
if (labelled || fromKnownSender) return;
thread.addLabel(label);
if (ARCHIVE) thread.moveToArchive();
});
}
function knownAddresses_() {
const known = new Set([Session.getActiveUser().getEmail().toLowerCase()]);
GmailApp.getAliases().forEach(function (alias) {
known.add(alias.toLowerCase());
});
let pageToken;
do {
const page = People.People.Connections.list('people/me', {
personFields: 'emailAddresses',
pageSize: 1000,
pageToken: pageToken,
});
(page.connections || []).forEach(function (person) {
addEmails_(known, person);
});
pageToken = page.nextPageToken;
} while (pageToken);
do {
const page = People.OtherContacts.list({
readMask: 'emailAddresses',
pageSize: 1000,
pageToken: pageToken,
});
(page.otherContacts || []).forEach(function (person) {
addEmails_(known, person);
});
pageToken = page.nextPageToken;
} while (pageToken);
return known;
}
function addEmails_(known, person) {
(person.emailAddresses || []).forEach(function (email) {
known.add(String(email.value).toLowerCase());
});
}
function addressOf_(from) {
const match = from.match(/<([^>]+)>/);
return (match ? match[1] : from).trim().toLowerCase();
}
The script checks your Google Contacts and Other contacts (people you have emailed) on a timer, so new mail can sit in the inbox for a few minutes. Apps Script has daily quotas and a 6-minute limit per run, and you maintain the code yourself. To let a sender through, add them to Google Contacts; mail you move back keeps its label and is not archived again.
Install the script
- Open script.google.com and select New project.
- Delete the sample code and paste the script.
- Next to Services, select the plus sign, choose People API, and select Add.
- Select screenInbox in the toolbar, select Run, and allow access to your Gmail and contacts.
- Open Triggers (the clock icon), select Add Trigger, choose screenInbox, a time-driven Minutes timer, and Every 10 minutes, then save.
Gmail filter, Apps Script, or KeepKnown?
| Question | Gmail filter | Apps Script | KeepKnown |
|---|---|---|---|
| Knows your Google Contacts | No, only addresses you type | Yes, reads contacts on each run | Yes, plus VIP domains and people you have replied to |
| Keeps up with new contacts | No, edit the filter by hand | Yes | Yes |
| When mail is checked | On arrival | Every few minutes on a timer | As mail arrives |
| Setup | A few minutes in Gmail settings | Paste a script, add a trigger | Connect Gmail with Google authorization |
| Where strangers go | A label you choose | A label you choose | The KK:OUTSIDERS label; nothing deleted |
| Cost | Free | Free | From $12.99/month |
| Main limit | The list goes stale and gets long | Quotas, timers, and code you maintain | Paid plan; Gmail and Outlook only |
Define who counts as known once.
People you know
Contacts and people you have already replied to can pass without maintaining one rule per address.
Organizations you trust
Approve a domain or sender group when an entire team should reach the inbox.
Everyone else
First-time and unfamiliar senders move to KK:OUTSIDERS for review. Nothing is deleted.
Build from this decision
Start with “sender is not known.”
Combine contact membership with approved domains, prior replies, or exceptions in one readable filter.
Starts paused. Preview recent matches before activation.
A safe workflow for first-time senders
- Connect Gmail or Google Workspace with Google authorization.
- Choose the relationships that make a sender known: contacts, approved domains, prior replies, or sender groups.
- Preview recent matches and add exceptions for expected account alerts or important first-time senders.
- Save the rule paused, then activate it when the preview looks right.
- Review unfamiliar mail in KK:OUTSIDERS and approve any sender who should pass next time.
Use your own inbox
Test the rule before it moves anything.
Preview which recent messages match, then keep the filter paused until you are ready.
No card required. No inbox changes during the audit.
Questions before you connect.
Can Gmail filter senders not in my contacts?
Gmail can filter individual senders, words, and labels, but it does not offer a native rule for every sender who is not in your contacts. Use -from: exclusions, an Apps Script that reads Google Contacts, or KeepKnown.
How do I filter emails that are not from someone in Gmail?
Put -from:name@example.com in the Has the words field of a filter. Add one -from: entry for each sender or domain to exclude, test the search, then select Create filter.
Is there an Apps Script that labels emails not in my contacts?
Yes. The script on this page reads Google Contacts with the People API and labels inbox mail from everyone else. Run it on a 10-minute time-driven trigger.
Does KeepKnown delete or permanently block email?
No. KeepKnown routes outsider mail to a separate label so important messages from new senders can still be found.
What if a legitimate first-time sender is not in contacts?
The message stays recoverable in the outsider review label. Approve the sender or add them to contacts when future messages should pass.
Does contact-based filtering read the email body?
No. The core relationship rule uses sender metadata, contacts, approved domains, prior correspondence, and your decisions.
Gmail unknown senders
Move senders not in your contacts out of the inbox automatically.