Hi Can any One Help me in Sending e mail .
I am creating code for Forget password where iam sending the password to those eho forget based on their Email iD. It Partially works but Sends Error as

Cannot send password to your e-mail address ie error 3

and my code is

<?$email=mysql_real_escape_string($_POST['email_to']);

$sql="SELECT `user_pass` FROM tbl_user WHERE 
user_email='".$email."'";
$result=mysql_query($sql,$con);

$count=mysql_num_rows($result);
if (isset($_POST['Submit']))
{
if($email == "")
{
$error ="Enter Email";
}

elseif($count==1){

$row=mysql_fetch_array($result);
{

$password = $row['user_pass'];

}

$to=$email;

$subject="Your password here";

$header="from: B2Bassociates";

$messages= "Your password for login to our website \r\n";
$messages.="Your password is $password \r\n";
$messages.="more message... \r\n";

$sentmail = mail($to,$subject,$messages,$header);

}

else {
$error1 = " Not Found ";
}

if($sentmail){
$error2 = "Your Password Has Been Sent To Your Email Address.";
}
else {
$error3 = "Cannot send password to your e-mail address";
}
}
?>

Dani AI

Generated

Good to see the thread got fixed for after ’s troubleshooting and ’s debugging tip. For anyone landing here later: the PHP mail() call can return a boolean that only tells you whether PHP handed the message off to the local MTA — it does not guarantee delivery. Common causes for “mail not sent” are incorrect headers, no MTA configured on the server, or delivery rejections (spam filters, missing SPF/DKIM, wrong envelope sender).

Quick practical checklist to diagnose and harden sending:

  • Make the From header a real e-mail address (for example: From: YourSite <no-reply@yourdomain.com>). Bare names are often rejected.
  • Verify PHP mail configuration (sendmail_path on Linux, SMTP/smtp_port on Windows) and ensure an MTA is running. See the PHP mail() documentation for details: PHP mail().
  • If mail() returns true but mail never arrives, check server mail logs (Postfix/Exim/Sendmail logs) or ask your host for delivery/bounce logs.
  • Check spam folders and ensure domain DNS has SPF/DKIM set for better deliverability. Consider setting the envelope sender (return-path) if bounces are an issue.

For production use, switch to authenticated SMTP via a maintained library (PHPMailer or similar). Libraries give clear SMTP error messages, support TLS, and improve reliability. PHPMailer: PHPMailer GitHub.

Security note (critical): never email plain-text passwords. Use a password-reset flow with a cryptographically secure, time-limited token and store passwords hashed (use PHP’s password_hash/password_verify). See: password_hash.

These steps will help isolate whether the problem is local PHP configuration, the mailserver, or deliverability, and will modernize and secure the password-reset flow.

Recommended Answers

All 6 Replies

Try replacing the last couple of lines with the following:

if($sentmail){
$error2 = "Your Password Has Been Sent To Your Email Address.";
}
else if (isset($sentmail)){
$error3 = "Cannot send password to your e-mail address";
}
}
?>

Glad to see code tags with syntax properties used on large chunks of code for once.:)

Hi,

Try to debug your code as why the email is not going...
As far as mail function is concerned, it return true if mail is sent and returns false if mail is not sent.

So just use

echo $sendmail

And see what value is coming and then use if function.

Hi Cwarn23,
No changes to ur solution.
Still throws the same error as Cannot send password to your e-mail address Thanks for ur reply.

Hi Varun,
As u told, i made the changes and echos the email but it didnt displays any thing. I Dont know where i went wrong .Can u help me Plz.


Thanks

If it isn't displaying anything chances are the true/false statement is not being stored. So perhaps the following might be better:

<?$email=mysql_real_escape_string($_POST['email_to']);

$sql="SELECT `user_pass` FROM tbl_user WHERE 
user_email='".$email."'";
$result=mysql_query($sql,$con);

$count=mysql_num_rows($result);
if (isset($_POST['Submit']))
{
if($email == "")
{
$error ="Enter Email";
}

elseif($count==1){

$row=mysql_fetch_array($result);
{

$password = $row['user_pass'];

}

$to=$email;

$subject="Your password here";

$header="from: B2Bassociates";

$messages= "Your password for login to our website \r\n";
$messages.="Your password is $password \r\n";
$messages.="more message... \r\n";

if(mail($to,$subject,$messages,$header)){
$error2 = "Your Password Has Been Sent To Your Email Address.";
}
else {
$error3 = "Cannot send password to your e-mail address";
}
}

else {
$error1 = " Not Found ";
}
}
?>

Ya it works good cwarn23
Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.