Gmail doesn’t support filtering or for that matter blocking emails from people not in your contact list. That is to mean, should your email address get in the hands of some unscrupulous entities, your poor inbox stands to get bombarded with spam or worse, phishing attacks.
This is a much-needed feature that seems to have escaped Google despite its obvious security benefits.
Recently I came across a stranger with this predicament and out of curiosity I wondered whether it would be possible to accomplish that using Gmail’s Filter options.
So for the better part of today I’ve been playing around with various filter settings and I seem to have chanced upon a capable workaround: rather than filter emails of those not in your contacts, how about we whitelist the existing contacts instead.
To do that we need to create a filter that will allow only emails from your existing Google contacts to reach your inbox while the rest will be sent to the trash (deleted), archived, forwarded etc. as they arrive.
Sounds good, doesn’t it? Let’s see how to do it then. Be warned though, much of this will be done manually so be prepared to set some time apart, and secondly, this is only suitable if you’ve a small contact list. This is because of Gmail’s filter 1500-character limit, which we can do nothing about.
Step 1: Select Contacts to Whitelist
- Log in to your Gmail Account, then switch to Google Contacts by clicking on the Contacts icon on the right side of the page.
- Inside Google contacts, select the contacts you’d wish to receive emails from. If it’s all of them, select one then at the top select the checkmark to select all.
- Just above the contacts, click on the Send Mail icon as you would were you sending an email to all the selected contacts. If some contacts have phone numbers only, Gmail will just ignore them and only add those with valid email addresses.
- Doing that will open a compose mail pop-up window with the selected contacts added as recipients.
- Now we need to copy the added contacts in the format used by Gmail. To do that, click the more button to reveal all the contacts, then click anywhere inside that box and press Ctrl + A.
- Open a text editor like notepad and paste the email addresses there. You can then discard the email.
Step 2: Create a Filter to Block Emails
- Go back to Gmail and open its Settings.
- Select the Filters and blocked addresses tab and click on the Create a new filter link.
- That should open a small Filter pop-up window at the bottom of the Search bar.
- Now we need to create a search term from the copied email addresses in the following format:
from:([email protected] OR [email protected] OR [email protected] OR [email protected]). Note that the OR operator has to be in caps and the from: at the start must be included as it indicates that the terms are email senders.
- The copied addresses will be in the format
John Doe <[email protected]>, Jane Doe <[email protected]>, Jack Screw <[email protected]m> …. So basically you have three things to get rid of:
- The names at the start (this is the trickier part)
- The commas which you can replace with the OR i.e., find
,and replace with
- The two arrows which you can batch remove quickly by replacing them with nothing.
- Take your time and create the search term. It shouldn’t take you that long if you only have a handful of contacts. On the other hand, if you have a slightly longer list, you may consider using Find and Replace or a Regular Expression to create the term quickly. However, please note the following:
- The search term has a character limit of 1500 characters. So ensure to keep it below this otherwise Gmail will reject the term. A text editor with a character counter (such as Notepad++ or Sublime Text) can help you in this.
- If you’ve a long contact list that exceeds this limit, then this workaround may not be suitable for you as stated earlier. This is because creating multiple filters will not work as Gmail applies them independent of each other, which is to mean it will block contacts in the other filters.
- Once you’ve your contacts in the search term, paste it inside the Doesn’t have box.
- To preview which emails would be blocked by this filter, click on the search button. All the emails not in your contact list will be filtered out.
- You can also do the opposite test to confirm that emails only from your contacts will be allowed. To do that, paste the search term inside the From box but exclude the from: part at the start. Doing this should display emails only from the included contacts.
- Once you’re done, click the button at the end search bar to restore the filter window. Next, click on the Create filter button at the bottom of the filter window.
Step 3: Choose a Filter Action
Next, you’ll be provided with a list of options of what to do with emails that don’t match the search term. The obvious choices here are to have them either Skip the Inbox or to Delete them.
With Skip the Inbox the emails won’t show up in the inbox and instead will be archived the moment they arrive. The archived emails can be accessed from the All Mail label (not the [Imap]/Archive).
If you chose to Delete them, they will be sent to the Bin automatically. As usual, they’ll be deleted completely if they stay in there for more than 30 days.
If you want the existing emails not from your contacts to be applied that filter, put a check mark on the Also apply filter to matching messages.
Once you have selected the option, click on the Create filter button to finish. The filter will now be active, however you can edit it any time by going to the Filters and blocked addresses settings.
You can also export this filter and import it in other Gmail Addresses.
If you happen to get a new contact that you’d wish to allow, just edit this filter by adding their email address to the search term inside the Doesn’t have box, then update the filter (don’t forget to add the OR operator).
You can also delete the filter anytime should you wish to stop filtering out the non-contacts from your inbox.
I know this is not the most intuitive way to do this, but in the meanwhile, I hope it meets your needs.
Update: For an automated solution, take a look at EmailGurus. I’m yet to test it, but the author claims it does exactly what I’ve outlined here. It’s open source, and you can refer to its GitHub to learn more on how it works and review its code.