Question

I am attempting to do some custom email forwarding. #1 I am on a shared server, which is using qmail, and optionally procmail.

What I am trying to accomplish is some custom email forwarding of one address, based on a schedule. For example, I have a schedule of employees that are working on each weekday, and I have a php script that selects a random employee's email address that is working today.

So I'm trying to just call that script in procmail, and output the result(which is a single email address) on the forward line:

Example:

#Something
LOGFILE=/usr/home/myname/procmail-log
VERBOSE=yes
EXITCODE=99
MAILDIR=/usr/boxes/myname
DEFAULT=/usr/boxes/myname/mybox
SHELL=/bin/sh
MYVAR=$(php -q /usr/home/myname/testemail/emailtester.php)

:0
! $MYVAR

This is not working. At all. I have also tried:

MYVAR=`php /usr/home/myname/testemail/emailtester.php`

as well as just piping it into the forward line:

:0
! |php /usr/home/myname/testemail/emailtester.php

I am COMPLETELY out of my element here... I tried to not even use procmail, and I just piped the whole email over to a php script, from qmail. I need the headers to stay intact, like a normal forward, and that proved to be difficult with PHP, and a little beyond my scope.( I managed to create an infinite email loop) So, I would rather not try that again.

I could just try to script this in perl, which I have never used, but I need the schedule to be administerable from a web interface, or at least in a user friendly way.

Any help, or suggestions would be appreciated at this point, thanks

EDIT:

Well, since I cant put code in a comment, I'll just edit here.

Now getting this in my log:

Folder: /usr/local/bin/php /usr/home/idnani/testemail/emailtester.ph     1679
"rocmail: Executing "/usr/local/bin/php,/usr/home/idnani/testemail/emailtester.php
Could not open input file: /usr/home/idnani/testemail/emailtester.php

When I use with :0fw I get:

"rocmail: Executing "/usr/local/bin/php,/usr/home/idnani/testemail/emailtester.php
Could not open input file: /usr/home/idnani/testemail/emailtester.php
procmail: [69907] Thu Jun 16 14:04:17 2011
procmail: Program failure (1) of "/usr/local/bin/php"
procmail: Rescue of unfiltered data succeeded

EDIT: Figured it out!

Found the correct way after MUCH trial and error.

Don't even use the pipe at, all, and you do need the ! forward symbol:

Final Rule:

:0
! `/usr/local/bin/php -f $HOME/emailtest/emailtester.php`

So simple... I'm a little mad it took me hours to figure this out, thanks everyone for helping to point me in the right direction!

Was it helpful?

Solution 3

Found the correct way after MUCH trial and error.

Don't even use the pipe at, all, and you do need the ! forward symbol:

all this rule does is ask the php script for a single email, and then forwards to that email. Final Rule:

:0
! `/usr/local/bin/php -f $HOME/emailtest/emailtester.php`

So simple... I'm a little mad it took me hours to figure this out, thanks everyone for helping to point me in the right direction!

Michael's answer looks like it should work, and maybe it does under a different circumstance? I couldn't get it to, which is why I am answering this myself..

New edit:

Note: You can still retrieve the whole email in php via STDIN

Note: you can put /usr/local/bin/php in your script itself like this on the first line:

#!/usr/local/bin/php

OTHER TIPS

You were so close... You need to pipe it without the ! forward:

:0
|/usr/bin/php /usr/home/myname/testemail/emailtester.php

EDIT I put in the full path to PHP, in case procmail's $PATH is incomplete. Change it to whatever your actual php path is.

To avoid email loops, I've done stuff like this:

PATH=/usr/local/bin:/bin:/usr/bin
MAILDIR=$HOME/Mail
DEFAULT=$HOME/Mail/inbox
LOGFILE=$HOME/procmail.`date +%Y-%m`.log
SHELL=/usr/bin/ksh

MY_XLOOP='X-Loop: emailtester.php'

:0
* ! ^$MY_XLOOP
{
    # add a header
    # 'f' = filter: continue processing results of program
    # 'w' = wait for program to return
    # 'h' = pass message headers to program
    :0fwh
    | formail -A "$MY_XLOOP"

    # then forward the message
    # 'c' = send a copy to recipient and continue processing
    :0c
    | php /usr/home/myname/testemail/emailtester.php
}

# if we get here, then the message has an X-Loop header.
# let it fall into $DEFAULT
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top