Quantcast
Channel: Active questions tagged email - Stack Overflow
Viewing all articles
Browse latest Browse all 29762

Password recovery using PHP mail

$
0
0

When I tried the following code snippet online, it did not send my password back. I think there is something wrong with my script but I do not know what. I am using my own server.

<?php
    $connection = mysqli_connect('localhost', 'xxxxxx','xxxxxx', 
    'xxxxx');
     if (!$connection){
         die("Database Connection Failed" . mysqli_error($connection));
      }
    $select_db = mysqli_select_db($connection, 'xxxxxxxx');
    if (!$select_db){
 die("Database Selection Failed" . mysqli_error($connection));

}?>
<?php
 if(isset($_POST) & !empty($_POST)){
$username = mysqli_real_escape_string($connection, $_POST['username']);
$sql = "SELECT * FROM users WHERE username = '$username'";
$res = mysqli_query($connection, $sql);
$count = mysqli_num_rows($res);
if($count == 1){
    echo "Send email to user with password";
}else{
    echo "User name does not exist in database";
}
}?>
<?php
 $r = mysqli_fetch_assoc($res);
$password = $r['password'];
$to = $r['email'];
$subject = "Your Recovered Password";
$message = "Please use this password to login " . $password;
$headers = "From : xxxx@caiyok.com";
if(mail($to, $subject, $message, $headers)){
echo "Your Password has been sent to your email id";
}else{
    echo "Failed to Recover your password, try again";
}
?>
<body>

<form class="form-signin" method="POST">
    <h2 class="form-signin-heading">Forgot Password</h2>
    <div class="input-group">
  <span class="input-group-addon" id="basic-addon1">@</span>
  <input type="text" name="username" class="form-control" placeholder="Username" required>
</div>
<br />
  </form>
 </body>

Viewing all articles
Browse latest Browse all 29762

Trending Articles