PDA

View Full Version : PHP forgotten password email


dahliacreative
02-26-2011, 04:55 PM
Firstly I am new here so Hello all!

I followed Dave's tutorial on creating a login script which was very useful as prior to today I had no php or mysql knowledge what so ever and I managed to get it working! So thumbs up to Dave for the tutorial!

Anyways, I've tried to create a forgotten password reminder script. So far I think it is working but I'm not actually getting any emails through so my question is, if you submit the request when you are running the site locally will the mail function still work or does it have to be hosted up on the net?

If it should still work from running locally on my machine then I've obviously done something wrong and may need your guys help to fix it.

Many thanks.

Simon.


Here is my code:

<?php
require_once("../Connections/login.php"); //Database Connection

//Catch Field Data
$email = $_POST['email'];
$submitted = $_POST['submitted'];

if($email) {
$query = sprintf("SELECT * FROM user where email='$email'");
$result = @mysql_query($query);
$rowAccount = @mysql_fetch_array($result);
}

if ($rowAccount) {

$subject = "Your eVites Username / Password Reminder";
$headers = "From: support@eVites.com";
$user = $rowAccount['user'];
$password = $rowAccount['pass'];
$msg = "<h1>My eVites Admin</h1>
<p>Hello '$user'!</p>
<p>Here is the username/password reminder you requested. If you didn't request this reminder then don't panic too much, the likely hood of someone gaining access is minimal. However, if you didn't request this reminder then obviously someone is keen to get into your account so we suggest that you log in to your My eVites admin and change your password immediately. We would also suggest that you change your email password just in case.</p>
<p>Username: '$user'</p>
<p>Password: '$password'</p>
<p>Many thanks, the eVites Support Team.</p>
";

$success = mail($email, $subject, $msg, $headers);

if($success) {
echo "<p id=errors>Reminder Success: Your Username and Password have been emailed to $email";
}
} elseif ($submitted) {
echo '<p id="errors">Reminder Failed: The email you entered was not found on the system, please try again.</p>';
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Username and Password Reminder</title>
<link rel="stylesheet" href="interface/css/default.css" media="all" />
<link href='http://fonts.googleapis.com/css?family=Droid+Serif:regular,italic,bold' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Droid+Sans:regular,bold' rel='stylesheet' type='text/css'>
</head>

<body>
<div id="login">
<h1>Username / Password Reminder</h1>
<form name="login" method="post" action="<?php $_SERVER['PHP_SELF'];?>">
<input name="email" type="text" id="email" />
<input name="submitted" type="hidden" value="1" />
<input type="submit" value="Remind Me!" class="formButton" />
</form>
</div>
</body>
</html>

edbr
02-28-2011, 12:29 AM
you need a form for email to be sent to a script that checks the email , and if exists retrieves the password from the same record and mails it to the email address

edbr
02-28-2011, 06:15 AM
try this

Forgotten Password? <form action="forgotten_password.php" method="POST">

<input name="email" type="text" />
<input name="submit" type="submit" value="submit" />

</form>
and forgotten_password.php

$email = $_POST['email'] ;

$sql = mysql_query("SELECT * FROM TABLE where email = '$email'")
or die(mysql_error());
$row = mysql_fetch_array($sql);
$rownum = mysql_num_rows($sql);

$to= $row['email'];
$subject= "Your password";
if(!$rownum ) {
echo "We can not find your email in our records";
}
if($rownum ==1 ){



$message .= "Name:" . $row['name']. "\n\n";

$message .= "Your password is:" . $row['password']. "\n\n";

$header = "";

$sent = mail($to,$subject,$message,$header);


if($sent)
{
print "Your password has been sent to your e mail address" ;
}


}

dahliacreative
02-28-2011, 09:40 AM
That's what I've done, I've just done it all in one page.
I'm running it from my local test server but it isn't sending the email, so I just wanted to know if it isn't sending because it's on my local machine or if I've done something wrong with regards to the send email code.

Thanks again.

davidj
02-28-2011, 12:45 PM
Please remember to protect your system against SQL injection

http://phpsec.org/projects/guide/

edbr
03-01-2011, 12:05 AM
good link david thanks

edbr
03-01-2011, 12:43 AM
That's what I've done, I've just done it all in one page.
I'm running it from my local test server but it isn't sending the email, so I just wanted to know if it isn't sending because it's on my local machine or if I've done something wrong with regards to the send email code.

yes unless you have a working mail server on your local machine it will not send mail

dahliacreative
03-28-2011, 01:09 PM
Ah ok cool, thank you. Glad that is the case as I couldn't see anything wrong with my code!!!

chrissyk
07-30-2012, 12:28 AM
I am using dahliacreative's code to email forgotten passwords but it always says that the email failed when I know it's in the database.....help?

dahliacreative
07-31-2012, 08:30 AM
Can you show us your code?

chrissyk
08-01-2012, 02:23 PM
<?php require_once('../Connections/connNWMMEA.php'); ?>
<?php

//Catch Field Data
$email = $_POST['Email'];
$submitted = $_POST['submitted'];

if($email) {
$query = sprintf("SELECT * FROM school_districts where Email='$email'");
$result = @mysql_query($query);
$rowAccount = @mysql_fetch_array($result);
}

if ($rowAccount) {

$subject = "Your NWMMEA.net Username / Password Reminder";
$headers = "From: someone@sjsd.k12.mo.us";
$user = $rowAccount['txt_username'];
$password = $rowAccount['txt_password'];
$msg = "<p>Here is the username/password reminder you requested. If you didn't request this reminder then don't panic too much, the likely hood of someone gaining access is minimal. However, if you didn't request this reminder then obviously someone is keen to get into your account so we suggest that you log in to your account and change your password immediately. We would also suggest that you change your email password just in case.</p>
<p>Username: '$user'</p>
<p>Password: '$password'</p> ";

$success = mail($email, $subject, $msg, $headers);

if($success) {
echo "<p id=errors>Reminder Success: Your Username and Password have been emailed to $email";
}
} elseif ($submitted) {
echo '<p id="errors">Reminder Failed: The email you entered was not found on the system, please try again.</p>';
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Username and Password Reminder</title>
<link rel="stylesheet" href="interface/css/default.css" media="all" />
<link href='http://fonts.googleapis.com/css?family=Droid+Serif:regular,italic,bold' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Droid+Sans:regular,bold' rel='stylesheet' type='text/css'>
</head>

<body>
<div id="login">
<h1>Username / Password Reminder</h1>
<form name="login" method="post" action="<?php $_SERVER['PHP_SELF'];?>">
<input name="Email" type="text" id="Email" />
<input name="submitted" type="hidden" value="1" />
<input type="submit" value="Remind Me!" class="formButton" />
</form>
</div>
</body>
</html>

chrissyk
09-23-2012, 10:58 PM
No ideas?? Help??

domedia
09-24-2012, 01:30 PM
Hi Chris,
what do you need help with?

chrissyk
09-24-2012, 11:43 PM
Thanks for replying.
You know I actually got it figured just last night after making that post.
Woo hoo!