Filters for cPanel email


I was attempting to create a filter in cPanel that would forward all addresses starting with "mj" followed by either an underscore or a year after 2020 directed at the domain "mjphd.net".


The first step is to select the mailbox for the default email on the domain mjphd.net. This is done in the cPanel controls under the email heading.

Select filter. It generates a complete list of all email accounts on the hosted domains.

Select the email box set as the default for the mjphd.net domain.

I'm a big user of email aliases. This allows me to create an email identity that I can kill should a service be compromised. As an example, I created josabank@mjphd.net when I placed an order. They lost control of their email list and the spam was rampant. To rid myself of the spam, I simply killed the alias. This works well but requires I create a new address for each site by creating the email alias in cPanel, a bit cumbersome. I moved to using wildcards. Starting an address with "mj_" and then adding something else provides an on-the-fly method for creating a throw away email. Should the service be compromised, or should I just tire of the emails, it is easy to create a filter on the server to discard the email or route it to junk.

The email "mj_test@mjphd.net" doesn't resolve since there is no mailbox or alias. Email sent to that address will go to the default email for the mjphd.net domain. Selecting that email and creating a filter allows the email to go to a monitored email box. Here is how it is done:

  1. select "email filters" from within cPanel
  2. click "manage filters" for the default email box for the domain mjphd.net
  3. a window will open showing existing filters for that mailbox; select create filter
  4. under rules, there are two drop-down menus; make the selection "Any recipient" in the first and "matches regex" in the second
  5. type ([M,m][J,j][_,2])\w+@mjphd.net
  6.  under actions, select "Redirect email" and type the address where you want the emails to go
  7. click the plus sign to the far right of Actions and make the selection Discard message
  8. click the plus sign to the far right on that same line and select "Stop processing"
  9. click "save"
  10. test the filter by replacing the "test@localhost" in the Filter Test and then clicking the Test Filter button. It will show where delivery will occur.

Looking at the ([M,m][J,j][_,2])\w+@mjphd.net is useful. The square brackets define two choices, M or m, J or j, and underscore or 2. They are in parentheses indicating they are all together. The \w matches any word character and the plus sign adjacent means any number. Bringing up the rear is  @mjphd.net to match. It could have been enclosed in parentheses, but they are optional. The regex test site listed below is incredibly helpful for playing around.


cPanel docs: https://docs.cpanel.net/knowledge-base/email/how-to-configure-email-filters/

dPanel docs on global filters: https://docs.cpanel.net/cpanel/email/global-email-filters/

the Webmaster: hhttps://www.thewebmaster.com/how-to-setup-cpanel-user-level-filtering-for-email/

Jonny Fox info on RegEx: https://medium.com/factory-mind/regex-tutorial-a-simple-cheatsheet-by-examples-649dc1c3f285

GitHub cheat sheet based on above: https://gist.github.com/ahmadelgamal/d3ea09a794856a386aeb949ef7a91f4e

RegEx test site: https://regexr.com/