skip to Main Content

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


  1. 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:

    /usr/bin/php -f /var/www/somefolder/script.php iscron=1 subject=stackoverflow
    

    The script.php can be:

    <?php
    
    unset($argv[0]);
    parse_str(implode('&',$argv),$_REQUEST);
    
    if ($_REQUEST["iscron"]=="1") {
      // do things for iscron=1
    }else{
      // do other things
    }
    
    // $_REQUEST["subject"] will be "stackoverflow"
    
    ?>
    
    Login or Signup to reply.
  2. 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.

    :0
    * ^To:(.*<)[email protected]|^Subject.*/(REMOVE|Undelivered Mail)
    | /usr/bin/php -f /var/www/somefolder/script.php -s "$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.

    :0
    * ^[email protected]
    | /usr/bin/php -f /var/www/somefolder/script.php
    :0E
    * ^Subject.*/(REMOVE|Undelivered Mail)
    | /usr/bin/php -f /var/www/somefolder/script.php -s "$MATCH"
    

    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 malicious php binary earlier in your PATH (in which case they could probably replace /usr/bin/php just as easily).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search