E-mail filtering in cPanel

Back

Home >> Email >> E-mail Filters

This article describes how to set up e-mail filters in cPanel.

About e-mail filters

You can use e-mail filters to block unwanted messages based upon a certain set of conditions. For example, you can specify that messages containing a certain word or phrase are deleted automatically or routed to another e-mail address. The e-mail filters that you create are cumulative: if an e-mail message passes through one filter, it can still be blocked by a subsequent filter.

There are two types of e-mail filters that you can create:

User-level filtering: You can configure e-mail filters for specific e-mail accounts.
Account-level filtering: You can configure e-mail filters that apply to all e-mail accounts for the domain.

 

 

Creating an e-mail filter

To create an e-mail filter for an e-mail account, follow these steps:

1.In the Mail section of the cPanel home screen, click User Level Filtering or Account Level Filtering.


2.If you are configuring a user-level filter, locate the account for which you want to add a filter, and then click Manage Filters.

3.Click Create a New Filter.


4.In the Filter Name text box, type the filter name.


Note:You should use a filter name that is easily identifiable so you can tell what the filter's purpose is. Also, filter names must be unique; you cannot use duplicate filter names.


5.Under Rules, specify the filter rules. For example, to filter e-mail messages that contain the word buy in the subject:
In the first list box, select Subject.
In the second list box, select contains.
Type buy in the text box.


Note:Filters use strict logic in determining how to handle messages. Make sure that your filter rules are not too broad. Otherwise, you may unintentionally block messages that you want to receive. Also, the text in rules is not case-sensitive. A rule that specifies buy and a rule that specifies BUY are equivalent.

6.Click the + icon to add another rule.


Note:You can add as many rules as you want. For example, the sample rule in step 5 blocks all messages that contain the word buy in the subject. This is a very broad rule that will likely block some legitimate messages. You can add another rule that specifies the word now in the subject. Select and in the list box. The filter will now match any message that contains the words buy and now in the subject line.


7.Under Actions, select the action that the filter should perform if rule conditions are matched. You can specify multiple actions.

8.Click Create.

 

Filter rules and criteria
The first set of options specifies which part of the email message the system examines to determine whether the message matches your filter parameters.
You can choose from the following options:

Option
Description
From The message sender's address.
Subject The message's subject line.
To

The address to which the sender sent the message.

 

Note:

Generally, the recipient does not receive the BCC field in an email's header. For this reason, you cannot use the BCC field in a filter.

 

Reply Address The address at which the sender receives replies.
Body The message's content.
Any Header Any part of the message's header.
Any recipient Any recipient of the message.
Has not been previously delivered The system only examines messages that remain in the queue for delivery.
is an Error Message The system only examines error messages that an auto-response system sends.
List ID The account's mailing lists.
Spam Status Whether Apache SpamAssassin™ marked the message as spam. The Spam Status line begins with Yes or No.
Spam Bar The content of the Spam Bar header that Apache SpamAssassin generated for this message. The more plus signs (+) that Apache SpamAssassin assigns to a message, the greater the likelihood that the system marks the message as spam.
Spam Score

The total number of plus signs (+) in the Spam Bar value, expressed as an integer.

 

 

Operators
After you select the portion of the email message that the system will examine, select the type of comparison between that portion of the email and the criteria that you enter.
You can choose from the following options:

 

Option
Description
equals The message exactly matches a defined string.
matches regex

The message matches a regular expression that you define.

Note

The filter text box accepts regular expressions when you select this option, rather than more commonly-used wildcard characters (for example, * or ?).
contains The message a string that you define.
does not contain The message does not contain the defined string.
begins with The message begins with the defined string.
ends with The message ends with the defined string.
does not begin The message does not begin with the defined string.
does not end with The message does not end with the defined string.
does not match The message does not exactly match the defined string.

 

After you have created a filter, you should test it to make sure that it filters what you intended.

To test an e-mail filter, follow these steps:

Option
Description
is above (#s only) The message's Spam Score is greater than the number that you define.
is not above (#s only) The message's Spam Score is equal to or less than the number that you define.
is below (#s only) The message's Spam Score is less than the number that you define.
is not below (#s only) The message's Spam Score is greater than or equal to the number that you define.

Important:
When you create a filter that uses several operators, the system processes the and operator before the or operator. For example:
The system processes A or B and C as A or (B and C).
The system processes A and B or C as (A and B) or C.

 

Criteria
Enter the criteria to use in the text box under the Rules menus. The type of data that you enter determines the comparison that the system performs.
For example, if you select the From and Equals filters, enter user@example.com as the criteria. The system determines that any email from user@example.com matches the filter, but does not match seconduser@example.com because it contains additional letters.


Important:
Exim converts newlines into spaces in the $message_body field. Do not add \n characters to any body filters that you create.
Actions
When cPanel determines that an email message matches your filter, it handles that message with the any of the following actions that you specify:

Action
Description
Discard Message The system discards the incoming message with no failure notice.
Redirect to email The system forwards the message to another email address that you specify.
Fail with message The system discards the message and automatically send a failure notice to the sender.
Stop Processing Rules The system skips all filter rules.
Deliver to folder The system delivers the message to a specified folder.
Pipe to a program The system sends the incoming message to a specified program. For more information, read the Pipe to a Program section below.

Pipe to a Program
Important:
Make certain that your script uses the proper file permissions (0700). To change your script's file permissions, run the chmod 0700 myscript.php command, where myscript.php represents your script's location and file name.


Use the Pipe to a Program option to parse and enter email information into a different system. For example, use the Pipe to a Program option to pipe email information to a program that enters email information into a ticket system.


STDIN pipes email and headers to the program.
Pipes can accept variables from the $_SERVER array and variables on the command line.
The language or environment that you use may cause memory limit issues.
If your script produces any output, even a blank line, the system will create a bounce message that contains that output.


When you use the Pipe to a Program option, enter a path that is relative to your home directory. For example, to use the /home/user/ script.pl script, enter script.pl in the Pipe to a Program text box, where user represents your username.


Note:If you use PHP, make certain that you appropriately wrap your code in start and end tags.


PHP and Perl hashbangs
To ensure that the script functions properly, the script must be executable and the appropriate hashbang line must exist at the top of the script.
If you use PHP, enter the following hashbang line at the top of the script:

  • #!/usr/local/bin/php -q
  • If you use Perl, enter the following hashbang line at the top of the script:

    #!/usr/bin/perl

If your script does not include a hashbang, enter the path to the Perl or PHP interpreter in the Pipe to a Program text box. Follow it with the full path to the script, for example:

/usr/local/bin/php /home/john/test.php
/usr/bin/perl /home/john/test.pl

Testing an e-mail filter

1.In the Mail section of the cPanel home screen, click User Level Filtering or Account Level Filtering.


2.If you are configuring a user-level filter, locate the account that has the filter you want to test, and then click Manage Filters.

3.In the Filter Test text box, type a test e-mail message.

4.Click Test Filter. The Filter Trace page shows if any rule conditions were met, and whether or not any actions will be taken.

 

Editing or deleting an e-mail filter

After you have created a filter, you can edit it or delete it.

To edit or delete an e-mail filter, follow these steps:

1.In the Mail section of the cPanel home screen, click User Level Filtering or Account Level Filtering.

2.If you are configuring a user-level filter, locate the account that has the filter you want to edit or delete, and then click Manage Filters.

3.Under Current Filters, locate the filter you want to edit or delete.
To edit the filter, click Edit. The Edit a Filter page is almost identical to the page that you use to create a filter.
To delete the filter, click Delete, and then click Delete Filter to confirm the deletion.