2

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!

Ben
  • 745
  • 7
  • 23

3 Answers3

4

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.

Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
  • Hmmm... Close! It's telling me it can't open the php file: Folder: /usr/local/bin/php -q /usr/home/idnani/testemail /emailtester 1677 "rocmail: Executing "/usr/local/bin/php,-q,/usr/home/idnani/testemail/emailtester.php Could not open input file: /usr/home/idnani/testemail/emailtester.php – Ben Jun 16 '11 at 17:58
  • @Ben I'm not sure whose permissions procmail is running with, but possibly it doesn't have read access to what looks to me like your home directory. – Michael Berkowski Jun 16 '11 at 18:11
  • Need to see what user procmail is running as, and/or if php has any restrictions (in terms of stuff like openbasedir, or suphp,etc.. since you said it is shared hosting) – Doon Jun 16 '11 at 18:11
  • Hmmm.... I'm not sure how exactly to see which user it is running as, trying to look that up. However, it can write to the log file just fine, which is also in the home directory. I could run a piped php script just fine from the qmail rules file – Ben Jun 16 '11 at 18:17
  • ok, procmail is being run with the right user ID, I think. if i set a variable in procmail to id -u, it shows up as the same uid as when i execute id -u in ssh. I have also tested executing the php script in ssh which works perfectly. I'm runnig out of other things to try – Ben Jun 16 '11 at 19:40
  • @Ben Perhaps try converting the PHP script into an executable. Add the shebang line (`#!/usr/local/bin/php -q`) at the top and `chmod +x`. Then call it directly from procmail `|/path/to/script.php` – Michael Berkowski Jun 16 '11 at 19:42
  • @Ben Oh, do you run SELinux? That could be getting in your way too! `setenforce 0` to temporarily disable. `setenforce 1` to turn back on. – Michael Berkowski Jun 16 '11 at 19:44
  • 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! – Ben Jun 16 '11 at 19:51
  • @Ben Odd, that's directly at odds with examples here & elsewhere (http://lipas.uwasa.fi/~ts/info/proctips.html) and my own experience – Michael Berkowski Jun 16 '11 at 19:53
  • @Ben Does your php script accept the input from STDIN or as a command line argument? – Michael Berkowski Jun 16 '11 at 19:56
  • Well, the php doesn't need to receive input at all, as it is just an email selector. If i wanted filtering rules to happen there, I would need to get the input there. When I was trying to handle the forwarding through php, just using qmail, I was recieving via STDIN – Ben Jun 16 '11 at 19:59
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
Tom Regner
  • 6,856
  • 4
  • 32
  • 47
Ben
  • 745
  • 7
  • 23
2

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
glenn jackman
  • 238,783
  • 38
  • 220
  • 352