Editing Email Subject Before Redirect

anoncubed

Registered
Oct 23, 2019
1
0
1
Los Angeles, CA
cPanel Access Level
Root Administrator
Can I edit incoming email subject before the redirect?

I currently have a catch-all email address setup. If an email address in my domain does not exist, redirect to my Gmail account.

I configured my Gmail to add the label "INVALID" to any redirected emails:

Gmail Filter:
If sent to *@mydomain.com label INVALID
unless it has these words (list of email addresess I created on my domain)

My Gmail filter is so long now. Gmail is preventing me to edit it anymore. What I ended up doing is creating smaller versions of the same filter, but limiting the email address list to about 15 each filter. This broke the filter altogether. Gmail labels anything coming from @mydomain.com as INVALID now.

I was thinking if I can have cPanel check if the email address exist first before the redirect, I can edit the subject line to append "INVALID" at the beginning so it's easier for me to filter it when it arrives to my Gmail.

If there is no option, do you guys have any suggestions for a workaround?

Thank you.
 

cPanelLauren

Product Owner
Staff member
Nov 14, 2017
13,296
1,272
313
Houston
Hi @anoncubed


I believe this is possible using a similar method we use to mark spam mail with the ***SPAM*** flag, but I do believe you'll have to specifically tell the filter what to do with the email (i.e., route to gmail)

  • Since you're going to have to modify the subject, the only place that is permissible is in the exim system filter.
  • You'd need root access to the server to be able to make modifications to the exim system filter
  • Almost all modifications I make here would require SSH access as well.
  • Instructions on how to make modifications to the system filter file and what I used as a guideline can be found here: How to Customize the Exim System Filter File - cPanel Knowledge Base - cPanel Documentation

Here's the steps I took to make this work in my test:

  1. Go to /usr/local/cpanel/etc/exim/sysfilter/options/ and create a new file - I called mine test, you can call yours whatever works for you
  2. Within the new file create your filter rules, below is what I created and tested:
    Code:
    #unrouted_mail 
    if 
    $header_to: contains "mydomain.com"
    and
    $header_to: does not contain “emailuser1”
    or $header_to: does not contain "emailuser2"
    or $header_from: does not contain "emailuser3"
    then
    headers add "New-Subject: [Invalid] $h_subject:"
    headers remove subject
    headers add "Subject: $h_new-subject:"
    headers remove new-subject
    deliver "[email protected]"
    seen finish
    endif
  3. Go to WHM>>Service Configuration>>Exim Configuration Manager -> Filters -> Enable the new rule you created
  4. I ran /scripts/buildeximconf after this but I also made a lot of edits after my filter was enabled.
  5. I then checked my Gmail address to ensure it was delivered with the [Invalid] tag added

Keep in mind this is pretty rudimentary as far as the initial test conditions and might need some reworking but should work for what you're looking for. You can check errors/do troubleshooting by checking the exim logs at /var/log/exim_mainlog
 
Last edited: