Procmail setup, to execute PHP script

陌路散爱 提交于 2021-01-24 19:06:20

问题


I've got a procmail setup working pretty well, seems to be executing my PHP script no problem when it receives an email.

Here is an example of the .procmailrc file:

#BEGIN PROCMAIL SCRIPT FOR MAIL PARSING
DEFAULT=$HOME/Maildir/
MAILDIR=$HOME/Maildir
PMDIR=$HOME/.procmail
LOGFILE=$PMDIR/log.`date +%y-%m-%d`
SHELL=/bin/sh

:0
|`/usr/local/php53/bin/php /home/usrmail/email/script.php`

The log output below:

 From x13542053@homiemail-mx22.g.dreamhost.com  Mon Feb 18 20:49:35 2013
 Subject: TEST
  Folder: HELLO                                                            2559
/bin/sh: HELLO: No such file or directory

Is just a simple echo "HELLO"; in the php script, seems to work fine!

However when I try to actually parse the email using the following code I get the error below in the log:

$rawEmail = '';
if (($fp = fopen('php://stdin', 'r')) !== false) {
    while (!feof($fp)) {
        $rawEmail .= fread($fp, 1024);
    }
    fclose($fp);
}

 $email = new Zend_Mail_Message(array(
   'raw' => $rawEmail
));

From actualaddress@gmail.com  Mon Feb 18 20:44:36 2013
 Subject: Re: Test
  Folder: Fatal error: Class 'Zend_Mail_Message' not found in /home/sy     2747
/bin/sh: Fatal: No such file or directory

This isn't working correctly obviously, I'm not sure why. For one, the From header went through on this one but not the other, that seems intermittent.

Also the directory (/home/sy) is either truncated or something is cutting it off to cause an error.

I'm not familiar with Zend really at all, or procmail. I was happy enough to get this far, just want to parse the email a bit so I can fetch the body of the email and put it somewhere. Someone convinced me this would be a better way than just using IMAP or something and I listened. If anyone's got any wonderful solutions or alternatives I'm all ears. Thanks guys!


回答1:


You are mixing two related but different pieces of syntax.

If you want to pipe to your script, it should be just

:0
* conditions, maybe
| /path/to/script

or if you want to use the output of the script, something like

:0
* conditions, maybe
`echo HELLO`

would file into a folder named HELLO, i.e. use the output from the script as a literal.

As for the error message from PHP, I imagine you need to add something to PHP's library path (quick googling suggests you should fix the include_path in your php.ini).

What are you trying to accomplish, though? If you just want to send the message where the headers say it should be going, something like

:0
* conditions, maybe
! -t

should get you that. I cannot imagine a situation where you would want to do this (other than if you are trying to solve the wrong problem altogether). If you want to send a truncated copy of the message, something like

:0c
* conditions, maybe
{
    :0fw
    | head -n 10
    :0
    ! -t
}

would truncate the message to the first ten lines. If you want to truncate to 1024 bytes exactly, that's not much harder.

On the other hand, if you just want to store the message's body (full RFC822 body, i.e. any MIME attachments etc will just be included verbatim, undecoded) you can do that with

:0b
saved/

or maybe if you want PHP there

:0b
| /path/to/script.php

For what it's worth, the error message is being truncated, but that is mainly because you are trying to use it as the name of the script to deliver the email to. If you take out the backquotes, the error message should end up in Procmail's standard error without truncation.



来源:https://stackoverflow.com/questions/14950223/procmail-setup-to-execute-php-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!