Help

How to create an email filter in cPanel

A mail filter is a set of rules that cPanel executes for incoming messages: moves them to the necessary folder, forwards them to another address, or rejects them altogether. The criteria can be a specific sender's address or, for example, words in the subject line of the email. Using filters will help maintain order in incoming messages and save time on manual email sorting.

How to create a filter

Find the "Mail" section on the cPanel home page and open the "Email Filters" application.

In the table "Filters for Each User," find the row with the necessary mailbox and click "Manage Filters."

Click "Create a New Filter" to open the filter creation wizard.

Step 1. Come up with a name

Come up with a brief name that reflects the essence of the filter, for example, "Letters from Accountants" or "HR." You can also use repeated text from the subject line of the email in the filter name, based on which the sorting is done.

Step 2. Create a rule

A rule in a mail filter consists of three components:

  • Criteria - specifies which elements of the incoming message the rule should apply to, such as the sender's address, words in the subject, or message text.
  • Condition - specifies what value of the criteria should be present for the rule to be triggered, for example, the specified word should be at the beginning of the subject, at the end, or anywhere in the subject line.
  • Value - specifies what text, email address, or number should match the specified condition.

Set up the necessary criteria, conditions, and values to create rules in the filter.

The following criteria settings for email filters can be selected from the left drop-down list:

  1. From - email address of the sender;
  2. Subject - email subject;
  3. To - email address of the recipient specified in the "To" field (addresses of additional recipients are not considered);
  4. Any recipient - any recipient of the email;
  5. Reply - email address to which the sender receives replies;
  6. Message text - email content;
  7. Any header - any part in the email header, including To, From, Envelope-To, Content-Type fields;
  8. Not previously delivered - the rule applies only to messages that are in the delivery queue;
  9. Error message - the rule applies only to error messages that the system sends automatically;
  10. List ID - mailing list number;
  11. Spam status - whether the incoming message was marked as spam by Apache SpamAssassin™;
  12. Spam Bar - the content of the Spam Bar header that Apache SpamAssassin™ generated for the email;
  13. Spam score - the number of + signs that SpamAssassin assigned to the email.

Conditions for email filters are located in the right dropdown list and have the following translations:

  • contains
  • matches regex
  • does not contain
  • equals
  • begins with
  • ends with
  • does not begin
  • does not end with
  • does not match

In addition, there are four conditions that are only available for the "Spam score" criterion. Here's how they can be translated:

  • is above - greater than;
  • is not above - not greater than;
  • is below - less than;
  • is not below - not less than.

The values of these conditions can only contain numbers that indicate the number of "+" signs assigned to the message by SpamAssassin. The higher the value, the more likely the message is to be marked as spam.


Step 3. Choose an action

When the rule conditions are met, cPanel can perform the following actions:

Discard message - the system will reject the message without notifying the sender. They won't know that their message was not delivered.

Forward to another email address - the system will forward the message to the specified email address, and no copy of the message will be saved. If you need to save a copy of the message and forward it to another address, you should add an additional action "Move to folder" and select the "Inbox" folder.

Send error message - the system will reject the message and send an error message notification.

Stop processing rules - the system will stop executing subsequent rules and will not apply other filters to this message.

Move to folder - the system will move the message to the specified folder in your mailbox.

Redirect to program - the system will process the message and forward it to another program using the specified script. For example, you can forward customer emails to a ticket system.

The path to the script must be specified relative to the home directory. For example, to use the script /home/username/script.pl, you need to specify script.pl in the field that appears when selecting this action.


Step 3. Choose an action

When the rule's conditions are met, cPanel can take the following actions:

Discard message - the system will reject the message without notifying the sender. The sender will not know that their message was not delivered. 

Forward to another email address - the system will forward the message to the specified email address, and a copy of the message will not be saved. If you need to save a copy of the message and forward it to another address, you should add the additional action "Place in folder" and select the "Inbox" folder. 

Report an error - the system will reject the message and send an error notification. 

Stop processing rules - the system will stop executing subsequent rules and will not apply any other filters to this message. 

Place in folder - the system will move the message to the specified folder in your mailbox. 

Pipe to a program - the system will process the message and pass it on to another program using the specified script. For example, you can send customer emails to a ticketing system.

The path to the script must be specified relative to the home directory. For example, to use the script /home/username/script.pl, you should specify script.pl in the field that appears when selecting this action.


Multiple rules and actions

You can add more than one rule or action to the filter. To do this, click on the "+" sign on the right side of the screen.

It is recommended to create several simple filters instead of one complex filter. The mail server processes short rules more efficiently than long ones.


When selecting multiple rules, an additional condition will appear before each of them:

OR - the action will be triggered if the previous rule or the next one is met. 

AND - the action will only be triggered if both the previous and the next rule are met. The system processes the AND condition with higher priority than OR. For example:

The system will process rules 1 or 2 and then 3 in the following order: 1 or (2 and 3). 

The system will process rules 1 and 2 and then 3 or in the following order: (1 and 2) or 3.


How to use email filters

To manage email filters, you need to go to the corresponding section on your mail server and select the desired mailbox. Click on the "Manage Filters" button to access current filters and testing.


How to edit a filter

To edit a filter, find it in the "Current Filters" table and select "Edit". Change the parameters you need and save the changes by clicking the "Save" button.


How to delete a filter

To delete a filter, find it in the "Current Filters" table and select "Delete". Confirm the deletion on the next page.


How to test a filter

To check the performance of filters, you can use the "Filter Test" section. Enter values for the "To", "From" and "Subject" fields, as well as the message text that will be used for testing. Then click "Test Filter".

Suppose you have a rule to place all emails with the subject "New Order" in the "Orders" folder. To test the filter, enter "New Order" in the "Subject" field and click "Test Filter." The test results will be displayed on the next page.

Sub-condition is false: not first_delivery 
Condition is false: not first_delivery and error_message 
Condition is true: $header_subject: contains New Order 
Return-path copied from sender
Sender      = username@uashared05.twinservers.net
Recipient   = username@uashared05.twinservers.net
Testing Exim filter file "/home/username/etc/vashdomen.com/admin/filter"
Headers charset "UTF-8"
Deliver message to: "username+Archive"@uashared05.twinservers.net
Filtering set up at least one significant delivery or other action.
No other deliveries will occur.

Here's the information you'll see in the results of the email filter test:

  • Each condition from the filter with an indication of whether it was successfully executed (true) or not (false).
  • The sender's address and recipient's address of the message.
  • The path to the filter file on the server.
  • The encoding of the message headers.
  • Any message about changes in message delivery or delivery confirmation, as usual.
    • Filtering did not set up a significant delivery. Normal delivery will occur. - The system will deliver the message as usual.
    • Filtering set up at least one significant delivery or other action. No other deliveries will occur. - The system will deliver the message to another mailbox/folder, or there will be no delivery.

What's next?

We hope that you have successfully set up email filters and now your emails will be neatly organized into the appropriate folders. The next steps in working with email may include setting up an autoresponder or setting up forwarding for incoming emails.

If you have any problems or additional questions, don't hesitate to leave a comment on the article. We will try to help you in any situation.

Have more questions about Hosting?