Sending a Password by Email - Exercise

Contact Us or call 1-877-932-8228
Sending a Password by Email - Exercise

Sending a Password by Email

Duration: 20 to 30 minutes.

In this exercise, you will create a Password Reminder page that allows users to have their passwords sent to them by email.

  1. Open EMail/Exercises/PasswordReminder.php in your editor.
  2. This page contains a simple form that submits to the same page.
  3. In the else block of the if condition add code that sends the user her password, which you'll need to get from the Northwind database.
  4. BCC yourself on the email. You'll need this to check to see that your solution is working.
  5. Set the mail server to one to which you have access.
  6. If the email fails to send, output the error to the browser. Otherwise, let the user know the password has been sent.
  7. Test your solution in your browser.

Note: when running any script that sends out emails, you will need a mail server and access credentials. Check with your System Administrator or with your Internet Hosting Service Provider to ge the mail server information.


<meta charset="UTF-8">
<title>Password Reminder</title>
	if (!array_key_exists('Submitted',$_POST))
		Enter your email in the form below to have your password sent to you.
		<form method="post" action="PasswordReminder.php">
		<input type="hidden" name="Submitted" value="true"><br>
		Email: <input type="text" name="To" size="25"><br>
		<input type="submit" value="Send Password">
		$to = $_POST['To'];
		@$db = new mysqli('localhost','root','pwdpwd','Northwind');
		if (mysqli_connect_errno())
		  echo 'Cannot connect to database: ' . mysqli_connect_error();
		 	$query = "SELECT Password FROM Employees WHERE Email = '$to'";
		  	$result = $db->query($query);
			$password = $row['Password'];
			$message = "Your password is <u>$password</u>.";
			$mail = new PHPMailer();
			$mail->IsSMTP(); // send via SMTP
			$mail->Host = ''; //SMTP server. You'll need to set this.
		  	$mail->From = '';
			$mail->FromName = 'Northwind Admin';
			$mail->AddAddress($to); //change it to your email address to actually get the email! 
			$mail->IsHTML(true); // send as HTML
			$mail->Subject  = 'Password Reminder';
			$mail->Body = $message;
				echo "Your password has been sent.";
			 echo "We could not send your password.<br>";
			 echo "Mailer Error: " . $mail->ErrorInfo;