Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 06-27-2006, 05:31 AM php stdin mail help
Super Spam Talker

Latest Blog Post:
PSD Squirrel Launched
Posts: 933
Trades: 7
hi all,

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

<ep@DOMAIN.com> (expanded from <ep>): delivery via local: delivered to
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...
Code:
  $fd = fopen("php://stdin", "r");

  $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 ...
Code:
#!/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);
?>
Any help or pointers would be great.
many thanks for your time reading this.

Sir P
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
Sir P is offline
Reply With Quote
View Public Profile Visit Sir P's homepage!
 
 
Register now for full access!
Old 06-27-2006, 07:37 AM Re: php stdin mail help
ChancesAre's Avatar
Skilled Talker

Posts: 84
Trades: 0
The block of code which reads from stdin is commented, so it wont work.

This block:
PHP Code:
/* read the mail that is forwarded to the script ***
  $fd = fopen("php://stdin", "r");

  $email = "";
  while (!feof($fd)) {
    $email .= fread($fd, 1024);
  }
  fclose($fd);*/ 
ChancesAre is offline
Reply With Quote
View Public Profile
 
Old 06-27-2006, 07:40 AM Re: php stdin mail help
Super Spam Talker

Latest Blog Post:
PSD Squirrel Launched
Posts: 933
Trades: 7
hi
thanks for replying.

in the live script it is not commented out, i pasted that from another file. sorry about that

any ideas?
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
Sir P is offline
Reply With Quote
View Public Profile Visit Sir P's homepage!
 
Old 10-17-2008, 08:01 PM Re: php stdin mail help
NullPointer's Avatar
Will Code for Food

Posts: 2,784
Name: Matt
Location: Irvine, CA
Trades: 0
I hate to dig up this old thread but I seem to be having a similar problem. I'm trying to intercept emails from a specific account using this script:
PHP Code:
#!/usr/bin/php
<?php
/* mailin.php is chmod 755 */
// get the email from stdin
$mail "";
$fp fopen("php://stdin""r");
while (!
feof($fp))
{
        
$mail.= fgets($fp,4096);
}
fclose($fp);

//write mail to file
//emails.txt is chmod 777
$out fopen("emails.txt","a+");
fwrite($out$mail);
fclose($out);

//mail is being forwarded to |/home/username/public_html/site/mailin.php
?>
In all of my tests the emails are not written to the file. I do however recieve the emails in my inbox.

Anyone see what I'm doing wrong?

Edit:

My mail service also sent this email back to the account I was using for testing
Quote:
This message was created automatically by mail delivery software.

A message that you sent could not be delivered to one or more of its
recipients. This is a permanent error. The following address(es) failed:

pipe to |/home/username/public_html/site/priv/mailin.php
generated by email@domain.com
local delivery failed
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 10-17-2008 at 08:19 PM..
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 10-20-2008, 03:47 PM Re: php stdin mail help
NullPointer's Avatar
Will Code for Food

Posts: 2,784
Name: Matt
Location: Irvine, CA
Trades: 0
I found the problem. The hashbang I was using was incomplete, the correct hashbang is:
Code:
#!/usr/bin/php -q
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 02-12-2009, 01:54 PM Re: php stdin mail help
Skilled Talker

Posts: 54
Trades: 0
does it mean that to receive incoming mail one needs to have a server on his computer and not a windows operating system.i want to use windows xp or vista to receive incoming email parse it to a php script.i have xampp installed.
kani alavi is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to php stdin mail help
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.23991 seconds with 12 queries