need some help with a php script I have.
The php script uses php://stdin to read email and then it processes that email. This script is on a linux server.
I have the server setup so that when email is sent to a certain aliases it pipes the email to my php script, this bit works as the mail report shows me it does ...
Quote:
The Postfix program
command: /usr/bin/php /var/www/html/thepostoffice.php
So that is great, it works.. that took me a while fiddiling to even get that to work. Anyway so that works however the script does not seem to do anything with the email. I have error logging in the php script but it only logs errors on the mail processing, if php://stdin actually recieved an email. To test that php://stdin is picking up email I put a IF statement in that writes a simple confirmation message to a text file if it has got an email. As per below...
$email = "";
while (!feof($fd)) {
$email .= fread($fd, 1024);
}
fclose($fd);*/
if($fd){
$data = "mail recieved.\n";
$fp = fopen("result.txt", "w");
fwrite($fp, $data);
fclose($fp);
}
.however no error is returned to the file. It just seems like nothing happens, as it is not getting the mail as per above. I will paste below the rest of the script, though it is pretty much just processing of the mail, which it cant do with the actual mail
If anyone of you know what im doing wrong or if there is some otherway I need to tell the script it is email then please say as I do not really undestand stdin. I mean, do I need to put another command in the aliases file? It is being delivered using ...
Quote:
`/usr/bin/php /var/www/html/thepostoffice.php
BTW the ` is a pipe in the aliases file, i just cant type a pipe on my current keyboard .
The whole script ...
#!/usr/bin/php
<?php
//***** database details ********
xxxx HIDDEN
//**********************************
//*** this function will write to log.log some error details ***
function write_log($mesaj)
{
$handle = fopen("log.log","w");
fwrite($handle,date("Y-m-d h:i:s").": ".$mesaj."\n");
fclose($handle);
}
//**********************************
/* read the mail that is forwarded to the script ***
$fd = fopen("php://stdin", "r");
$email = "";
while (!feof($fd)) {
$email .= fread($fd, 1024);
}
fclose($fd);*/
//*** handle email ***
$lines = explode("\n", $email);
// empty vars
$from = "";
$date = "";
$subject = "";
$message = "";
$splittingheaders = true;
for ($i=0; $i<count($lines); $i++) {
if ($splittingheaders) {
// look out for special headers
if (preg_match("/^Subject: (.*)/", $lines[$i], $matches)) {
$subject = $matches[1];
}
if (preg_match("/^From: (.*)/", $lines[$i], $matches)) {
if(strpos($lines[$i],"<"))
{
//the name exist too in from header
$data = explode('<',$lines[$i]);
$from = substr(trim($data[1]),0,-1);
}
else{
//only the mail
$from = $matches[1];
}
}
if (preg_match("/^Date: (.*)/", $lines[$i], $matches)) {
$date = $matches[1];
}
} else {
// not a header, but message
$message .= $lines[$i]."\n";
}
if (trim($lines[$i])=="") {
// empty line, header section has ended
$splittingheaders = false;
}
}
//*** make a connection to the database: ***
$handle = mysql_connect($server,$username,$pass) or write_log("Can't connect to the database");
mysql_select_db($database) or write_log("Can't select database");
//**********************************
$when = date("Y-m-d G:i:s");
$data = explode('@',$from);
$username = $data[0];
mysql_query("insert into mails ( `username`, `from`, `subject`, `date`, `message`) values ( '$username', '$from', '$subject', '$when', '$message')") or write_log("Can't execute query");
//mysql_query("insert into mails values('','".$username."','".$from."','".$subject."','".$date."','".$message."')") or write_log("Can't execute query");
mysql_close($handle);
?>
Reference: http://www.webmaster-talk.com/php-forum/54358-php-stdin-mail-help.html