CLI file_get_contents / stdin

Hello! Very simple question (hopefully!)

When run in the command line, this code prompts for input but then just hangs.

echo "type something: ";
$text = file_get_contents("php://stdin", "r");
echo "you typed ".$text."\

Any ideas why it never reaches the “you typed…” line?

How are you executing the script? To send something via STDIN you would do myscript.php < path/to/file (or pipe the email into your script). Your first snippet was doing something else entirely (waiting for user input when prompted, which readline is for).

The script is executed by sending an email, /etc/aliases routes it to the script successfully and the script fires. Everything works as it should, but the content of the email sent to the script is not accessible using stdin…

Thanks, and sorry for not explaining that very well initially!

When prompted for input, it doesn’t seem to matter what I enter - a single word or an entire sentence - the script hangs.

The eventual aim for this is a PHP script which can read an email using php://stdin, i have folloed various tutorials but this part is beating me!

This is the complete script:

#!/usr/bin/php -q
$conn = mysql_connect ("xx.xx.xx.xx", "xxxxxxx", "xxxxxxx") or die ('Cannot connect: ' . mysql_error());

function get_email_addresses($text) {
	$email = array();
	$invalid_email = array();
	//remove all characters which can not feature in an email address, plus spaces
	$text = ereg_replace("[^A-Za-z._0-9@ -]"," ",$text);
	//break it down into "words"
	$token = trim(strtok($text, " "));
	while($token !== "") {
		if(strpos($token, "@") !== false) {		
			$token = ereg_replace("[^A-Za-z._0-9@]","", $token);
			//checking to see if this is a valid email address
			if(is_valid_email($email) !== true) {
				$email[] = strtolower($token);
			else {
				$invalid_email[] = strtolower($token);
		$token = trim(strtok(" "));
	$email = array_unique($email);
	$invalid_email = array_unique($invalid_email);
	return array("valid_email"=&gt;$email, "invalid_email" =&gt; $invalid_email);
function is_valid_email($email) {
	if (eregi("^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.([a-z]){2,4})$",$email)) return true;
	else return false;

// read the email from stdin
$fd = fopen("php://stdin", "r");
$text = "";
while (!feof($fd)) {
    $text .= fread($fd, 1024);

$arrays = get_email_addresses($text);

$clues = array("failed","failure","permanently","permanent","delivery","Delivery","rejected","recipient");
//make sure it's a bounce
foreach($clues as $this_clue){

foreach($arrays['valid_email'] as $this_valid){

	//deactivate it
	mysql_select_db("mailing_database", $conn);
	$sql_deactivate_str = "UPDATE `email_list` SET `active` = 0 WHERE `email` = '".$this_valid."'";
	$sql_deactivate= mysql_query($sql_deactivate_str, $conn) or die(mysql_error());


Everything works except the “read the email from stdin” part… if i replace it with a string, the rest of the code works fine.

Are you sending any input into the script? It looks like you’re really after something like readline.