Web Hosting / cPanel

How to Create Email Filters in CPanel

Jan 04, 2023
10 min read
Setup cPanel Email Filters

You can set cPanel Email filters from within the cPanel control panel dashboard. They can either be set globally or on a per-email address basis. They give you another level of control over what emails you receive and are particularly beneficial for helping prevent spam emails.

With the email filters, you can set conditions to target messages that contain a specific word or phrase, From, To, or based on the subject. You can add multiple levels of email filters to precisely fine-tune what you wish to happen with the email received.

The emails your filter discarded can be sent to another email address or delivered to a third-party program, such as a support ticketing system. cPanel email forwarding is often used for this purpose).

Email filters can be set either on a user level or globally. I’ll cover these in turn, even though they are virtually identical, apart from the first couple of steps.

I will then look at a couple of practical examples.

How to Setup cPanel Email Filters for an Individual Email Address

How to Setup cPanel Global Email Filters

Example: Using Multiple Email Filters with SpamAssassin

I have created a very [detailed SpamAssassin tutorial/spamassassin-email-spam-protection-in-cpanel/) which goes into detail about all the steps required to configure SpamAssassin to create a spam filter properly. It includes setting the SpamAssassin Score (Spam Threshold Score), moving spam to the junk folder (Spam Box), auto-delete, and adding emails to either the whitelist or blacklist.

When you create rules within the Spam Filters section, it will automatically create Global Email Filters. It can be easier to configure basic SpamAssassin rules through this interface.

You sometimes want more granular control over when SpamAssassin triggers an action. For example, you may wish for random newsletters you receive to be marked as spam unless the body matches the “keyword”. This may be a particular topic you are interested in.

I will now make a SpamAssassin rule that satisfies the following conditions:

  • Spam Score is less than 4.
  • The body does not contain the keyword “SEO”.

Using cPanel Email Filter Wildcards

You can use wildcards in Spam Filters by using Regex. Providing a full breakdown of how Regex works is beyond the scope of this tutorial.

However, I will provide you with some basic examples to get you started:

Anchors - ^ and $

  • ^The - matches any string that starts with “The”
  • end$ - matches a string that ends with “end”
  • ^The end$ - exact string match
  • filtering - matches any string that has the text “filtering” in it

Quantifiers - *, +, ?

  • abc* - matches a string containing “ab” followed by “zero” or more “c”
  • abc+ - matches a string that has “ab” followed by one or more “c”
  • abc? - matches a string that has “ab” followed by zero or one “c”
  • abc{2} - matches a string that has “ab” followed by 2 “c”
  • abc{2,} - matches a string that has “ab” followed by 2 or more “c”
  • abc{2,5} - matches a string that has “ab” followed by 2 up to 5 “c”
  • a(bc)* - matches a string that has “a” followed by “zero” or more copies of the sequence “bc”
  • a(bc){2,5} - matches a string that has “a” followed by 2 up to 5 copies of the sequence “bc”

OR operator - | or []

  • a(b|c) - matches a string that has “a” followed by “b” or “c”
  • a[bc] - matches a string that has “a” followed by “b” or “c”

Grouping - ()

  • a(bc) - parentheses create a group with the value “bc”

For more detailed examples see this cheat sheet.

Example Email Filter using a Wildcard

Let’s take the example I used previously configuring a SpamAssassin filter that contained several rules. I can simplify the rules match field as follows:

How to Test your Email Filters

On the main Email Filter Screen, you will see an option to “Test Filter.” You will see a preset simple email message that you can modify to test any of your Filters.

In my example below, I set a filter to delete any emails from [email protected].