In this case, the user has forgotten their login and or password. This script will email it to them or will email an admin because the email was not found. The update part works fine, but something is not right with the mail part.
newmemberpassword.html
<FORM METHOD="POST" ACTION="newpassword.php">
<P><font color="#0080C0"><strong><font size="2" face="Verdana">Email Address</font></strong><font face="Verdana"><STRONG><font size="2">:</font></STRONG><BR>
</font></font><font color="#0080C0" face="Verdana">
<INPUT TYPE="text" NAME="email" SIZE=25 MAXLENGTH=50></font></p>
<P>
<font color="#0080C0">
<INPUT TYPE="submit" NAME="submit" VALUE="Submit" style="font-family: Verdana"></font></P>
</FORM>
newpassword.php
include ('config.php');
$table_name="users";
$usermail=$_POST['email'];
//build and issue the query
$sql ="SELECT * FROM $table_name WHERE email = '$usermail'";
$result = @mysql_query($sql) or die(mysql_error());
//get the number of rows in the result set
$num = mysql_num_rows($result);
//If match was found, get username and email from database
if ($num != 0)
{
while ($sql = mysql_fetch_object($result))
{
$usermail= $sql -> email;
$uname= $sql -> member_login;
}
echo $usermail;
echo '<br />';
echo $uname;
echo '<br />';
//Update database with new password
$newpass = substr(md5(time()),0,6);
$chng = "UPDATE $table_name SET
member_password = '$newpass'
WHERE email = '$usermail'";
$result2 = @mysql_query($chng) or die(mysql_error());
echo $newpass;
echo '<br />';//these echo just fine, so I know this part is working.
$to = "$usermail";
$subject = "Your Username & Password";
$headers = "From: Customer Service \r\n";
$body = "Your username is $uname.\n";
$body .= "Your password is $newpass.\n";
if(mail($to,$subject,$body,$headers))
{
$msg = "<p>Your username & temporary password has been emailed to you.</p>";
$msg .= "<p>You must change this password immediately after your next login.</p>";
$msg .= "<p></p>";
echo $msg;
}
else
{
echo "Message Not Sent";
}
}
else
{
//If no email was found in the database send a notification to the admin
$to = "admin@mydomain.com";
$subject = "Missing Member Email";
$headers = "From: <www.domain.com>\n";
$body = "A user with the email address of $usermail has requested a username and password reminder.\n";
$body .= "$usermail could not be located in the database.\n";
if(mail($to,$subject,$body,$headers))
{
$msg = "<p>Your email address could not be located</p>";
$msg .="<p>The Website Administrator has been emailed, you should be contacted by them shortly.</p>";
echo $msg;
}
else
{
echo "Message Not Sent";
}
}
The results- it echos the username, password and email
and goes with the first message: Your username & temporary password has been emailed to you.
You must change this password immediately after your next login.
no email comes through.
Thanks!