I have a procmail script set up which pipes to a PHP script when an email subject line matches:
:0
* ^[email protected]|^Subject.*(REMOVE|Undelivered Mail)
| /usr/bin/php -f /var/www/somefolder/script.php
Is there a way to pass a variable, perhaps like you do with query-string parameters? I’ve tried the following, and neither works:
| /usr/bin/php -f /var/www/somefolder/script.php?iscron=1
| /usr/bin/php -f /var/www/somefolder/script.php --iscron=1
I want the PHP script to be able to check what is triggering it. Thanks for any help.
2
Answers
In PHP, to get the value(s) of parameter(s) passed from the command line, you may parse the
$argv
For further details, one may see official documentation
So assuming that your command (to pipe from procmail to the PHP) is:
The script.php can be:
Procmail offers the
/
capturing token to grab the matched string. This works vaguely like capturing parentheses in other regex implementations, but there can be only one, and it grabs everything matched by the regular expression after it and stores it in$MATCH
.If the
To:
header matched,$MATCH
will contain any previous value, or (likely often) be empty. If you want to work around that, maybe refactor to two recipes.The
E
in:0E
says to only attempt this recipe if the previous recipe’s condition(s) didn’t match. Notico also how I switched to use the^TO_
macro which handles a number of different recipient fields (To:
,Cc:
,Resent-To:
, etc etc)As you can see, I’m imagining that you would refactor your PHP script to take an option
-s <subject match>
.Tangentially, spelling out
/usr/bin/php
is probably excessive; unless you have reasons to believe that an attacker could somehow add a different maliciousphp
binary earlier in yourPATH
(in which case they could probably replace/usr/bin/php
just as easily).