php mail function not sending mail

My problem seems to be with the email variable I use in the mail() function.  When I hard code an email address it works fine.  for example mail("$em", ...... doesn't work but
mail("somemail@here.com", ...  works just fine.  I'm thinking I may be doing something wrong in my mysql query in getting the email address.  Thanks......
<?php

$cxn = mysqli_connect($host,$user,$passwd,$dbname)
             or die("Query died: connect");       


$email=$_POST['email'];
$email=mysqli_real_escape_string($cxn,$email);
$status = "OK";
$msg="";
error_reporting(E_ERROR | E_PARSE | E_CORE_ERROR);

if (!stristr($email,"@") OR !stristr($email,".")) {
$msg="Your email address is not correct<BR>";
$status= "NOTOK";}



if($status=="OK"){ 
$sql="SELECT email,username,password FROM member WHERE email = '$email'";

$result=mysqli_query($cxn,$sql)
                or die("Query died: email: ".mysqli_error($cxn));
$num=mysqli_num_rows($result);
$row=mysqli_fetch_row($result);

$em=$row->email;// email is stored to a variable

if ($num == 0) {  
echo "<center><font face='Verdana' size='2' color=red><b>No Password</b><br> Sorry Your address is not there in our database. You can signup and login to use our site. <BR><BR><a href='signup.php'> Sign UP </a> </center>"; 
exit;}

// formating the mail posting
// headers here 
$headers4="somemail@here";  
$headers.="Reply-to: $headers4\n";
$headers .= "From: $headers4\n"; 
$headers .= "Errors-to: $headers4\n"; 


// mail funciton will return true if it is successful
if(mail("$em","Your Request for login details","This is in response to your request for login details at The Trading Den \n \nLogin ID: $row->username \nPassword: $row->password \n\n Thank You \n \n Webmaster","$headers")){echo  "<center><font face='Verdana' size='2' ><b>THANK YOU</b> <br>Your password is posted to your emil address. Please check your e-mail after some time. </center>";}

else{// there is a system problem in sending mail 
echo " <center><font face='Verdana' size='2' color=red >There is some system problem in sending login details to your address. Please contact site-admin. <br><br><input type='button' value='Retry' onClick='history.go(-1)'></center></font>";} 

} 
else {// Validation failed so show the error message 
echo "<center><font face='Verdana' size='2' color=red >$msg <br><br><input type='button' value='Retry' onClick='history.go(-1)'></center></font>";}

?>

Open in new window

dmalovichAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
>>      $em=$row->email;// email is stored to a variable
WHY? The value stored in the db should be the same in $_POST['email']. So you should NOT even be selecting that value in your sql and instead use the one in $_POST:
<?php

$cxn = mysqli_connect($host,$user,$passwd,$dbname)
             or die("Query died: connect");       


$email=$_POST['email'];
$email=mysqli_real_escape_string($cxn,$email);
$status = "OK";
$msg="";
error_reporting(E_ERROR | E_PARSE | E_CORE_ERROR);

if (!stristr($email,"@") OR !stristr($email,".")) {
	$msg="Your email address is not correct<BR>";
	$status= "NOTOK";
}



if($status=="OK"){ 
	$sql="SELECT username,password FROM member WHERE email = '$email'";

	$result=mysqli_query($cxn,$sql) or die("Query died: email: ".mysqli_error($cxn));
	$num=mysqli_num_rows($result);
	$row=mysqli_fetch_row($result);

	//$em=$row->email;// email is stored to a variable

	if ($num == 0) 
	{  
		echo "<center><font face='Verdana' size='2' color=red><b>No Password</b><br> Sorry Your address is not there in our database. You can signup and login to use our site. <BR><BR><a href='signup.php'> Sign UP </a> </center>"; 
	exit;
	}

	// formating the mail posting
	// headers here 
	$headers4="somemail@here";  
	$headers.="Reply-to: $headers4\n";
	$headers .= "From: $headers4\n"; 
	$headers .= "Errors-to: $headers4\n"; 
	

	// mail funciton will return true if it is successful
	if(mail($_POST['email'],"Your Request for login details","This is in response to your request for login details at The Trading Den \n \nLogin ID: $row->username \nPassword: $row->password \n\n Thank You \n \n Webmaster","$headers"))
	{
		echo  "<center><font face='Verdana' size='2' ><b>THANK YOU</b> <br>Your password is posted to your emil address. Please check your e-mail after some time. </center>";
	}
	else
	{// there is a system problem in sending mail 
		echo " <center><font face='Verdana' size='2' color=red >There is some system problem in sending login details to your address. Please contact site-admin. <br><br><input type='button' value='Retry' onClick='history.go(-1)'></center></font>";
	} 

} 
else 
{
	// Validation failed so show the error message 
	echo "<center><font face='Verdana' size='2' color=red >$msg <br><br><input type='button' value='Retry' onClick='history.go(-1)'></center></font>";
}

?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dmalovichAuthor Commented:
The email was sent thanks.  I think the error is with $em=$row->email;  because in my mail message I have     Login ID: $row->username \nPassword: $row->password     and this information is not being sent. Do you know what the problem with this might be?  I will assign points but just wondering if you can answer my follow up question.
0
dmalovichAuthor Commented:
All set. Thanks
0
hieloCommented:
Suggestion: keep a window tab opened to the manual pages:

http://us2.php.net/manual/en/mysqli-result.fetch-row.php
  mysqli_fetch_row — Get a result row as an enumerated array

In other words, if you keep using fetch_row(), what you need is:$row[0], $row[1], etc.

so use:
http://us2.php.net/manual/en/mysqli-result.fetch-object.php

$row=mysqli_fetch_object($result);

and
$row->password should work.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.