Re: pipe mail / get input from stdin
| From: | Nicolas Windpassinger - Sun | Date: | Wed, 15 Nov 2000 08:33:27 +0000 |
| Subject: | Re: pipe mail / get input from stdin | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-25393@lists.php.net to get a copy of this message | ||
Dieter,
here is what i did. I wanted to parse an email and gets the different
fields. The email is piped into the script, it works perfectly, and no bugs
have been reported to me about it, hope it will help you:
I know there is french stuff, but the PHP code has no boundries ;-)))
Nicolas.:
#!/usr/local/bin/php -q
<?
#config
#on sort les variables du mail
$fp = fopen('php://stdin','r');
$fout = fopen("./fillupdb.html", "w");
$line=0;
fwrite($fout, "<html><body>");
while(!feof($fp)) {
$data=fgets($fp,4096);
$data=chop($data);
if (eregi("^Name: ",$data)) {$data=eregi_replace( "Name: ", "",
$data);$name=$data;}
if (eregi("^Owner: ",$data)) {$data=eregi_replace( "owner: ", "",
$data);$owner=$data;}
if (eregi("^Section: ",$data)) {$data=eregi_replace( "Section: ",
"",
$data);$section=$data;}
if (eregi("^Origin: ",$data)) {$data=eregi_replace( "Origin: ",
"",
$data);$origin=$data;}
if (eregi("^Version Date: ",$data)) {$data=eregi_replace( "version
date: ", "", $data);$verdate=$data;}
if (eregi("^Version Number: ",$data)) {$data=eregi_replace( "version
number: ", "", $data);$vernumb=$data;}
if (eregi("^Version Comments:",$data)) {$data=eregi_replace( "version
comments:", "", $data);$begin_comment="go";$ligne=$line;}
if ($data=="__END_OF_RECORD__"){$begin_comment="stop";}
$line=$line+1;
#begin to read after the version comment:
if (($begin_comment=="go") and (($ligne+1)==$line))
{ $ligne=$ligne+1;
$resultcomment.=$data." ";}
#on est a la fin d'un morceau
if ($begin_comment=="stop")
{
#on inscrit les datas dans fout
fwrite($fout,"-----------------------------<br>");
fwrite($fout,$name);fwrite($fout,"<br>");
fwrite($fout,$owner);fwrite($fout,"<br>");
fwrite($fout,$section);fwrite($fout,"<br>");
fwrite($fout,$origin);fwrite($fout,"<br>");
fwrite($fout,$verdate);fwrite($fout,"<br>");
fwrite($fout,$vernumb);fwrite($fout,"<br>");
fwrite($fout,$resultcomment);fwrite($fout,"<br>");
fwrite($fout,"-----------------------------<br>");
#on reinitialise les vars
$resultcomment="";
$begin_comment="";
$ligne="";
}
}
#on ferme les fichiers
fwrite($fout, "<html><body>");
fclose($fout);
fclose($fp);
?>
> Can someone explain, why this is working when started
> as a shell script manually on the shell (saves input from
> stdin properly to a file) but not when pipeing (e.g. utilizing
> /etc/aliases)an email into it (creates an empty file!)?
>
> On the other hand, the second (unix shell) script below
> works just fine when triggered by an email.
>
> The line in /etc/aliases is something like:
> xyz: "|/path/to/my/script"
>
> [Of course, I have PHP as CGI..]
>
> Thank you very much!!!
>
> dk
>
> ------------------ PHP SHELL SCRIPT --------------
> #!/usr/local/bin/php -q
> <?
> // tried all of these but without success!
> // $fp = fopen("php://stdin", "r");
> // $fp = fopen("/dev/fd/0", "r");
> $fp = fopen("/dev/stdin","r");
>
> while(($buf=fgets($fp, 512)) != false) {
> $dump .= $buf;
> }
> fclose($fp);
>
> $fpf = fopen("/tmp/mail.txt","w");
> fputs($fpf,$dump);
> fclose($fpf);
> ?>
>
> The above should work like this one:
> ------------------ SHELL SCRIPT --------------
> #!/bin/sh
> LOGDIR="/tmp/mail.txt"
> #
> # Read stdin
> #
> while read LINE
> do
> echo $LINE >>$LOGDIR
> done
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net