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?
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.