i'm having trouble with my code, i want it to send a user their username via email when they have forgotten it.
What it does:
- Retrieves user information from database
- Send HTML Email to User's email
What it doesn't do:
- Display the user's login username when sent to the email.
The PHP code is below for the page "forgot-username.php"
<?php
require_once('../../Connections/localhost.php');
require('../../PHPMailer/PHPMailerAutoload.php');
if(isset($_POST) & !empty($_POST)){
$username = mysql_real_escape_string($_POST['username'], $localhost);
$sql = "SELECT * FROM database.users WHERE users.email = '$email'";
$res = mysql_query($sql, $localhost) or die(mysql_error());
$count = mysql_num_rows($res);
if($count == 1){
$r = mysql_fetch_assoc($res);
$username = $r['username'];
$to = $r['email'];
$subject = "Your Recovered Username";
$message = file_get_contents("email_template.html");
$headers = 'From: Your name <info@address.com>';
if(mail($to,$subject,$message,$headers)){
header('Location: username-sent.html');
}else{
echo "Failed to Recover your email, try again";
}
}else{
echo "email does not exist in database";
}
}
?>
<!DOCTYPE html>
<!--[if IE 8 ]><html class="no-js oldie ie8" lang="en"> <![endif]-->
<!--[if IE 9 ]><html class="no-js oldie ie9" lang="en"> <![endif]-->
<!--[if (gte IE 9)|!(IE)]><!-->
<html class="no-js" lang="en">
<!--<![endif]-->
<head>
<!-- ====== Basic Page Needs ====== -->
<meta charset="utf-8">
<title>Forgot Username</title>
<!-- ====== Mobile Specs Meta ====== -->
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<!-- ====== CSS ====== -->
<link rel="stylesheet" href="../../css/base.css">
<link rel="stylesheet" href="../../css/vendor.css">
<link rel="stylesheet" href="../../css/main.css">
<link rel="stylesheet" href="../../css/zerogrid.css">
<!-- ====== Java Scripts ====== -->
<script src="../../js/modernizr.js"></script>
<script src="../../js/pace.min.js"></script>
<!-- ====== Favicon ====== -->
<link rel="shortcut icon" href="../../favicon.ico" type="image/x-icon">
<link rel="icon" href="../../favicon.ico" type="image/x-icon">
</head>
<body id="top">
<!-- ====== Header Begin ====== -->
<header>
<div class="header-logo">
<a href="#">Logo</a>
</div>
<a id="header-menu-trigger" href="#0">
<span class="header-menu-text" style="color: white">Menu</span>
<span class="header-menu-icon"></span>
</a>
<nav id="menu-nav-wrap">
<a href="#0" class="close-button" title="close"><span>Close</span></a>
<img alt="KBG" src="../../images/logo.png" style="height: 100px;width: 100px;" />
<ul class="nav-list">
<li class="current"><a class="" href="../../index.html">Home</a></li>
<li><a class="" href="../../about.html" title="">About</a></li>
<li>
<a class="" >Services</a>
<div>
<ul>
<li><a href="../../Service1.html">Service 1</a></li>
<li><a href="../../Service2.html">Service 2</a></li>
<li><a href="../../Service3.html">Service 3</a></li>
<li><a href="../../Service4.html">Service 4</a></li>
</ul>
</div>
</li>
<li><a class="" href="../../Service5.html" >Service 5</a></li>
<li><a class="" href="../../competition.html" >Competition</a></li>
<li><a class="" href="../../login.php" >Sign In</a></li>
<li><a class="" href="../../signup.html" >Sign Up</a></li>
<li><a href="../../contact-us.html">Contact Us</a></li>
</ul>
</nav> <!-- end #menu-nav-wrap -->
</header>
<section id="services">
<div class="overlay"></div>
<div class="zerogrid">
<div class="row">
<!--Start Box-->
<div class="col-1-3 offset-1-3">
<div class="wrap-col">
<div class="row">
<div class="animate-this">
<h1 style="color: white">Forgot username?</h1>
<p class="lead" style="color: white">Fill in your email below and we will send you your username.</p>
<?php if(isset($smsg)){ ?><div class="alert alert-success" role="alert"> <?php echo $smsg; ?> </div><?php } ?>
<?php if(isset($fmsg)){ ?><div class="alert alert-danger" role="alert"> <?php echo $fmsg; ?> </div><?php } ?>
<form method="post">
<div class="form-field">
<input name="email" style="color: white" class="full-width" type="text" id="email" placeholder="Email" value="" minlength="5" required>
</div>
<div class="form-field">
<input type="submit" class="submitform full-width" style="background-color: white; color: black" value="Send" name="forgotusername">
</div>
<a href="login.html" style="color: white">Go Back</a>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
<footer>
<div class="footer-bottom">
<div class="row">
<div class="col-full">
<div class="copyright">
<span>© Copyright Company 2017.</span>
</div>
</div>
</div>
</div>
<div id="go-top">
<a class="smoothscroll" title="Back to Top" href="#top">
<i class="fa fa-long-arrow-up" aria-hidden="true"></i>
</a>
</div>
</footer>
<div id="preloader">
<div id="loader"></div>
</div>
<!-- ====== Java Scripts ====== -->
<script src="../../js/jquery-2.1.3.min.js"></script>
<script src="../../js/plugins.js"></script>
<script src="../../js/main.js"></script>
<script src="../../js/cookiechoices.js"></script>
<script>
cookieChoices.showCookieBar({
linkHref: '../../cookie-policy.html',
language: 'en'
});
</script>
</body>
</html>
this is the code that is sent to the users email "email_template.html"
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<link rel="shortcut icon" href="https://www.website.com/favicon.png" type="image/x-icon">
<link rel="icon" href="https://www.website.com/favicon.png" type="image/x-icon">
<link rel="stylesheet" href="https://www.website.com/css/base.css">
<link rel="stylesheet" href="https://www.website.com/css/vendor.css">
<link rel="stylesheet" href="https://www.website.com/css/main.css">
<link rel="stylesheet" href="https://www.website.com/css/zerogrid.css">
</head>
<body style="max-width: 100%">
<section style="background-color: black; text-align: center; height: 20%; min-height: 5%"><img src="https://www.website.com/images/logo.png" alt="logo" style="max-height: 150px; max-width: 150px; margin-top: 1%" /></section>
<section id="services">
<div class="overlay"></div>
<div class="zerogrid">
<div class="row">
<!--Start Box-->
<div class="col-1-3 offset-1-3">
<div class="wrap-col">
<div class="row">
<div class="animate-this">
<h1 style="color: white">Forgot Your Username?</h1>
<p class="lead" style="color: white">Not a problem, we've searched our database for your username.</p>
<p class="lead" style="color: white">Your username is:</p>
<div class="form-field">
<input type="button" class="full-width" style="background-color: white; color: black" value="$username">
</div>
<br />
<br />
<p class="lead" style="color: white">Regards,<br />Support Team</p>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
</body>
</html>
Everything does as it should but the username doesn't show up, instead "$username" shows up in the email.
Someone please help. Thanks in advance.