Custom user exim filter with custom rules

Islandhosting

Active Member
May 15, 2015
25
4
53
Canada
cPanel Access Level
Root Administrator
Twitter
cPanel's filter editor does not provide all the functionality a client needs, so they have edited the /home/$user/etc/$domain/$mailbox/filter file manually.

In this case they need to send a brief notice to another offsite mailbox indicating new mail has arrived, without quoting the message, so they are using the exim "mail" command.

The filter works, but of course they can't use cPanel's filter editor to add/edit other rules because it wipes out the custom one. If there a way to put custom rules in a filter.local file or something along those lines that will be appended/prepended whenever someone uses the filter editor to make a change?
 

cPRex

Jurassic Moderator
Staff member
Oct 19, 2014
13,259
2,086
363
cPanel Access Level
Root Administrator
Hey there! Currently, no, there isn't a way to do that. However, this sounds like an excellent feature request. Could you get one submitted using the link in my signature, and then I can bring it up with the team next week?
 

sparek-3

Well-Known Member
Aug 10, 2002
2,126
258
388
cPanel Access Level
Root Administrator
Would a better solution be to set the email address to forward to a pipe and run a script to do the necessary action?

A feature request for every little knick-knack item can become burdensome and takes away from focusing on issues that really matter.
 

rbairwell

Well-Known Member
May 28, 2022
97
41
18
Mansfield, Nottingham, UK
cPanel Access Level
Root Administrator
I agree with @sparek-3 , using a script and a filter does the job quite easily.

I've just testing by creating a mailbox called "mytest", going to Manager->Manage Email Filters and creating a filter:
  • Filter name: test
  • Rules:
    • Has Not Been Previously Delivered
  • Actions:
    • PIpe to a program: | php -q /home/username/testscript.php
    • Deliver to Folder: /INBOX

and in /home/username/testscript.php I have:
Code:
#!/usr/bin/php -q
<?php
mail('[email protected]','test message','test');
Sending an email to the [email protected] email account results in an email with the subject "test message" and the body "test" being sent to [email protected] AND the email actually being stored in the "mytest" inbox.
 

sparek-3

Well-Known Member
Aug 10, 2002
2,126
258
388
cPanel Access Level
Root Administrator
A filter would work, but I would probably use a forwarder.

Use the Pipe to a Program option and put

testscript.php

in the text box. (Because it's relative to the user's home directory, i.e. assuming testscript.php is in /home/username

And then for the testscript.php script, I would use:

Code:
#!/usr/local/bin/php -q
<?php

fclose(STDOUT);
fclose(STDERR);
$STDOUT = fopen('/dev/null', 'wb');
$STDERR = fopen('/dev/null', 'wb');

mail('[email protected]','test message','test');
Then make sure the /home/user/testscript.php script has execution permissions set for user in the File Manager. The execute bit has to be set, because the interface doesn't allow you to specify an interpreter (i.e. /usr/local/bin/php) to run the script through. The shebang line has to be used and the execute bit has to be set.

The extra

fclose(STDOUT);
fclose(STDERR);
$STDOUT = fopen('/dev/null', 'wb');
$STDERR = fopen('/dev/null', 'wb');


at the beginning of the script, helps to insure that no output is printed to STDOUT or STDERR when executing through the pipe. You don't want any output, otherwise senders will get a bounce back message.