Page 1 of 1

php + stdin

Posted: Fri Aug 12, 2005 8:39 pm
by skolar
I'm trying to write a script that reads emails and acts as a filter for incoming emails.

When i send an email, i have it setup to go to the script with qmail and the script is being called. however, i cant seem to get the email (neither contents nor headers) sent to the script. From what i've seen, its supposed to be sent in php://stdin which is what i'm trying to read from.

Here's my code:

$fp = fopen('php://stdin', 'r');
$email = fread($fp, filesize('php://stdin'));
fclose($fp);

However i keep getting this error:

PHP Warning: filesize(): Stat failed for php://stdin (errno=2 - No such file or directory) in /home/httpd/vhosts/getbounty.com/redir.php on line 13

I dont know why this is happening.

Here is what i have in my .qmail file :
| /path/to/my/php/script.php


Any help would be much appreciated. Thanks.

Posted: Fri Aug 12, 2005 8:42 pm
by feyd
filesize() uses the stat() function to retrieve size information. stat() only works on existing files. php://stdin isn't a file, it's a stream, therefore has no size.

You are supposed to loop over the incoming lines until you reach the end of the email.

Posted: Fri Aug 12, 2005 8:47 pm
by skolar
awesome...it seems to work now. You i got the idea for using filesize from "The PHP Anthology - Object Oriented PHP Solutions: Vol 1"

Could it be a bug in the book?

Thanks again.

Posted: Fri Aug 12, 2005 8:58 pm
by feyd
from what I know, stdin has never supported and will never support stat(), so I call bug.