Gmail as a Spam Filter on Cpanel

    August 20, 2007

You probably have heard that you can use Gmail to filter spam for all your email addresses. You might have gone so far as to look up how to do it and end up a little frustrated because your not a server whiz and rely on cpanel to set up filters. The reason your having problems is because Cpanel is a bit different than a vanilla Linux install.

Quick Overview

Gmail has very effective spam filters that we can utilize to filter our non Gmail email. There are many ways to set up filtering with Gmail with Cpanel. One way to do this is to forward our mail to our Gmail address. Then we can respond using our shiny new Gmail account and have all our email filtered. If you want to continue to use your old email address though this doesn’t work well.

A second way to do this is to forward all email to Gmail and allow Gmail to send email as your original domain. Allowing another server to email on your domains behalf is called “relaying” and done wrong can be a source of spam. Smartly Google requires a special server setup that tells them they have permission to send email on your domains behalf. The setup is called SMTP AUTH and isn’t standard with Cpanel so it was out for me.

A third way is to set up our email to handled by Google. In order to do this you sign up for Google Apps and set up your dns records accordingly. I think this option is great for regular family sites or beginners but for my business. If you don’t have or are uncomfortable using shell access then this option might be best for you. I chose not to use this though because don’t want to be left high and dry if Gmail encounters any problems.

Lastly you can set up a series of rules at Gmail and on your server to filter your email. The first rule is set on your server and forwards your email to your Gmail account if its not been filtered yet. The second rule is setup at Gmail to forward email to your email server if its for your domain. The last rule is on your server that checks if the emails been to Gmail yet and then routes it to the appropriate mail box. This option works great but requires shell access to your server and can be tricky to setup with Cpanel. This is the option I chose to use.

How I Setup Gmail Filtering with Cpanel

I chose the series of rules option but I needed to modify it a bit to work with Cpanel. You can not add the rules using the Cpanel mail filter in the control panel because you can’t add all the necessary rules. Since thats the case you need to edit the filter file manually on the server. On a regular *Nix server with Exim as the mail server you would put a file called “.forward” in the root of your home directory. The “.forward” file would have all the rules you exim to process when it handles your mail. On a Cpanel server this doesn’t work though. There is a directory with files for filters for all the domains on a Cpanel machine. The directory is called “/etc/vfilters/” and the file name will be your domain name. With VI I opened and edited the file adding the rules noted here. Then I added the rule at Gmail to forward my mail back to my server.

Here is an example of my setup:

In the file “/etc/vfilters/”:

# Exim filter
if not first_delivery and error_message
then finish endif
if $message_headers: does not contain
"X-Forwarded-For:" then
deliver ""
deliver ""

In my Gmail account I went to the Settings page and then to the Filters tab. I set a filter that causes Gmail to forward all email addressed to a the domain “”, and keep a copy in Gmail’s inbox. To do it yourself just go to the advanced filters and enter your domain in the “to” field (no need for wildcards). Then select forward for the action and fill in the email address.

The Results

For the first couple of hours Gmail let through most spam but after marking the bad stuff off and on it catches most of it.  For me that means it stopped ~2000 spam mails  and let ~5 through this morning. To contrast these results with my previous spam filtering  I was receiving ~80 spams messages a day. With some more training I am confident that those numbers will get even better.


I hope this helps you set up Gmail filtering for yourself. I am sure there are other ways to do this this is just how I solved it. Also not that the solution I posted sends all the email back to one email address. That might not work for you if not then take a peek at the exim filter documentation. You should be able to come up with more complex filters and rules to forward email to more than one email address.