0

This doesn't work. It's supposed to email me.(as a test; what we really want the php file to do is insert into a mySQL database table)

The input is a payment notification message from PayPal.

(A similar recipe does find the Description line and mails it to two of us)

the procmailrc:

VERBOSE=off
MAILDIR=/usr/home/our/mail
LOGFILE=/usr/home/our/paypal.log
EXITCODE = 99

:0b
|base64 -d|grep -i Description|php tp129.php

the php file:

$rawmessage = "";
$fd = fopen("php://stdin", "r");
while(!feof($fd)) {
$rawmessage .= fread($fd, 1024);
}
fclose($fd);

$msg129 = 'raw is ' .$rawmessage;

mail('[email protected]','msg129',$msg129 );
1
  • 1
    At a high level, I'm concerned that you may end up in an infinite mail loop.
    – Jeff Schaller
    Jan 29, 2016 at 22:56

2 Answers 2

0

I see a few things you might want to fix:

  1. Check your $HOME/.forward file. I think it has to have 644 permissions, and contain a single line: "| /usr/bin/procmail" to get procmail to do things with your email. Check the actual path to procmail - it may be /bin/procmail or something else on your system. Supposedly you have to have the fully qualified path to procmail in .forward.
  2. Put <?php on the first line of the file tp129.php Also, your pipeline may need to have the full path to the php interpreter, /usr/bin/php or whatever.
  3. Double check the params in .procmailrc. The file names in MAILDIR=/usr/home/our/mail and LOGFILE=/usr/home/our/paypal.log seem fishy. That is, /usr/home/ is a strange directory. User ID's "home" directories are usually in /home or /u, very rarely if ever under /usr any more.
  4. Run the PHP code by itself: php tp129.php, and then type some text in, any text, then ctrl-D. Does that send email to the desired address(es)? If not, fix whatever causes that.
5
  • ls -l .f* -rw------- 1 bcp users 59 Mar 16 2006 .forward
    – Sheldon
    Jan 29, 2016 at 23:26
  • Hi, @Bruce Ediger ls -l .f* -rw------- 1 bcp users 59 Mar 16 2006 .forward Is that OK? The php file is alright, works fine if one pipes in text. The procmailrc is called paypal.rc pwd gives /usr/home/bcp Sorry, pressing Enter here seems to end the message? Thanks for your help. Please tell me any more info I can supply, and/or any ideas to try.
    – Sheldon
    Jan 29, 2016 at 23:37
  • OK, pwd says that, but what does echo $HOME say? And I think the file has to be named .procmailrc for the procmail process to find it. procmail may run with a very small PATH - is php in a directory in $PATH? My .procmailrc file has 644 permissions, somethings things get picky about that (sshd, for instance).
    – user732
    Jan 30, 2016 at 3:35
  • ~> echo $HOME /usr/home/bcp ~> echo $PATH /usr/home/bcp:/usr/home/bcp/bin:/bin:/usr/bin:/usr/games:/usr/X11R6/bin:/usr/local/bin:. ~> which php /usr/local/bin/php It is 644
    – Sheldon
    Jan 30, 2016 at 9:04
  • 1
    If you have a log file /usr/home/our/paypal.log, that should already show whether Procmail is running. I assume it is, and that the log file will actually contain information which shows you what's actually wrong (namely, that PHP does not send the message because it's malformed; or, possibly, that the script is being executed as a shell script, and thus producing dire syntax errors).
    – tripleee
    Jan 30, 2016 at 11:27
0

The PHP mail() function requires a valid email message, with full headers. It looks like your code only produces a mail body (unless the decoded base64 content contains the headers; but even then, adding an arbitrary string in front of them would wreck that message).

Your explicit assignment of EXITCODE would terminate Procmail with an error message if it weren't for the fact that it contains a syntax error. In shell script, and thus also in Procmail, you are not allowed whitespace around an equals sign in an assignment. It's unclear what you hope to achieve with the EXITCODE anyway; my suggestion is simply to leave it out.

Given that your final goal is to import the message to MySQL, I would propose a simpler test: Open a second file for appending, and write the output there. This should show you whether the PHP code is working as you expect. (Maybe add a time stamp so you see when the results were written, so you don't end up examining old results. Perhaps you'll actually want to overwrite instead of append as long as you're testing; but the date stamp still makes sense.) If PHP is your tool of choice, feel free to stick with that, although I suspect this would be much simpler as a tiny shell script, which you could probably inline to your .procmailrc. Depending on your database schema, something like

:0b
| base64 -d | \
  sed -n '/Description/s/.*/insert into table (field1,field2,field3) values (&);/p' \
  | mysql -uyou -pxyzzy -s db

In the meantime, here's how I would solve your immediate problem.

The entire PHP program can be replaced with a single piece of sed script: sed '1s/^/raw is /' though to avoid a useless use of grep I would also factor in the search for Description.

:0bfw   # Can be simplified if your sed supports a case-insensitive flag
| base64 -d | \
  sed -n '/[Dd][Ee][Ss][Cc][Rr][Ii][Pp][Tt][Ii][Oo][Nn]/s/^/raw is /p'
:0
! [email protected]

The forwarding action will retain the original headers. If that's not what you want, maybe extract the ones you do want to keep, e.g. with formail; or create entirely new headers with printf. You could replace the simple second recipe with the action ! [email protected] with something a bit more complex, like perhaps the following:

:0fhw
| ( formail -XSubject:; \
    printf "From: %s\nTo: %s\n" "[email protected]" "[email protected]" )
:0
! -t

(sendmail -t means take whatever the headers of the message say, and send the message there.)

For debugging, your recipe file should not contain an explicit VERBOSE=off. This is the default, which makes no sense to override; but hard-coding it makes it impossible to debug your recipes with VERBOSE=yes on the Procmail command line.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .