Link to home
Start Free TrialLog in
Avatar of dmalovich
dmalovich

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dmalovich
dmalovich

ASKER

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.
All set. Thanks
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.