By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,025 Members | 1,242 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,025 IT Pros & Developers. It's quick & easy.

Send mail to user if he forget the password

100+
P: 250
I'm developing web site using php and my sql. In there i want to send a mail including new password to user, when he forget his password. How can i send mail using php? Could you please help me?
Jun 9 '08 #1
Share this Question
Share on Google+
10 Replies


10K+
P: 13,264
There is a built in mail() function that is easy to use. Look it up, try the code and post again if you still have problems.
Jun 9 '08 #2

100+
P: 250
There is a built in mail() function that is easy to use. Look it up, try the code and post again if you still have problems.

Yes. I tried it. But got an error saying "Access denied for user 'root'@'localhost' (using password: NO)". Is there any setting for server? This is my code
[PHP]
<?php
$err=0;

if(isset($_POST['forgot'])){
$send = $_POST['forgot'];

$sql = "SELECT Email FROM user_admin WHERE Email = '".$_POST["email"]."'";
$rs_email = mysql_query($sql) or die(mysql_error());

if (mysql_num_rows($rs_email) > 0){
$send = $_POST['forgot'];
$err=0;
}
else{
$send="";
$err=1;
}
}

if ($send == "forgot"){

$letters = array("A","B","C","D","E","F","G","H","I","J","K", "L","M","N",
"O","P","Q","R","S","T","U","V","W","X","Y","Z","0 ","1","2",
"3","4","5","6","7","8","9","0");

$pass="";

$passlength=rand (8, 12);

for($i=0;$i<$passlength;$i++){
$pass.= $letters[rand (0, 35)];
}

$notencrypted=$pass;
$pass=md5($pass);

//echo $notencrypted;

$sql = "UPDATE user_admin SET Password ='".$pass."' WHERE Email = '".$_POST["email"]."'";


$body = "";
$body = $body."<p>Your password is : ".$notencrypted."</p>";

$subject = "Password Recovery";
$toemail = trim($_POST["my@gmail.com"]);

$headers = "From: Me <my@yahoo.com>\n";
$headers = $headers."Content-Type: text/html; charset=iso-8859-1\n"; // Mime type

mail($toemail, $subject, $body, $headers);

echo '<p>Your password has been changed and sent to your email address</p><br />';

}
else{
?>
<p>Please provide your email address and your password will be emailed to you</p><br />
<?php

if($err==1)
echo '<p style="color:#992928;">The email address that you were entered is not an existing one</p>';
?>
<form name="forgotpass" id="forgotpass" method="post" action="<? echo $_SERVER['PHP_SELF']; ?>" onsubmit="return validateforgot(this);">
<input name="email" id="email" type="text" size="25" /> <br />
<input name="forgot" value="forgot" id="forgot" src="images/submit.jpg" type="image" style="margin:5px 0 0 0;" />
</form>
<?php
}
?>
[/PHP]
Jun 10 '08 #3

ak1dnar
Expert 100+
P: 1,584
There is a permission problem or your user name/passwords are incorrect of your MySQL connection String. But how did you connect to your database? I can't see the lines of coding for that.
Jun 10 '08 #4

10K+
P: 13,264
There is a permission problem or your user name/passwords are incorrect of your MySQL connection String. But how did you connect to your database? I can't see the lines of coding for that.
Yep, if you call mysql_query() before mysql_connect(), PHP will use default values (specified in php.ini) to try and connect to the database. Those defaults are usually just a username with no password.
Jun 10 '08 #5

100+
P: 250
Yep, if you call mysql_query() before mysql_connect(), PHP will use default values (specified in php.ini) to try and connect to the database. Those defaults are usually just a username with no password.
Connect my sql using [PHP]include 'dbconnect.php';[/PHP] .
Jun 10 '08 #6

100+
P: 250
Can some one tell me how can i connect to remote mail server and test my code? Is there any way?
Jun 10 '08 #7

10K+
P: 13,264
Can some one tell me how can i connect to remote mail server and test my code? Is there any way?
Perhaps you are not understanding the problem here. That error message simply means that you are not getting access to your mysql database due to incorrect username/password. Try to solve that one first and see what happens.
Jun 10 '08 #8

100+
P: 250
Perhaps you are not understanding the problem here. That error message simply means that you are not getting access to your mysql database due to incorrect username/password. Try to solve that one first and see what happens.

Understood. I just try using
[PHP]<?php
$to = "my@gmail.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "my@yahoo";
$headers = "From: $from";
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>[/PHP]
This is working. I put this file to a server which has mail server. But i'm working on my local machine and is there any way to run that file on my machine with connecting mail server in another machine?
Jun 11 '08 #9

10K+
P: 13,264
Understood. I just try using
[PHP]<?php
$to = "my@gmail.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "my@yahoo";
$headers = "From: $from";
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>[/PHP]
This is working. I put this file to a server which has mail server. But i'm working on my local machine and is there any way to run that file on my machine with connecting mail server in another machine?
If the user can access the database from the database machine only then you need to grant them priviledges for remote access as well.
Jun 11 '08 #10

100+
P: 250
If the user can access the database from the database machine only then you need to grant them priviledges for remote access as well.

Still i'm confusing. The server which i hosted my web site doesn't have mail server. But There is another server which is having mail server. So i want to know how to connect that mail server using my website to send mail.
Jun 13 '08 #11

Post your reply

Sign in to post your reply or Sign up for a free account.