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: 15 to 25 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.cfm in your editor.
  2. Write code that looks for the user's password in the users table.
  3. If the password is found, email it to the user and return an appropriate message to the browser. Send the message in both HTML and plain text format.
  4. If the password is not found (i.e, there is no such email address in the users table), return a message saying no such user exists.
  5. You will need to specify a valid mail server.
  6. When you are done, try it out in your browser.

Code Sample:

Email/Exercises/PasswordReminder.cfm
<cfparam name="FORM.email" default="">
<html>
<head>
<title>Password Reminder</title>
</head>
<body>

<cfif NOT isDefined("FORM.submitted")>
	<h2>Password Reminder</h2>
	<cfoutput>
	<form method="post" action="#CGI.SCRIPT_NAME#">
	</cfoutput>
	<input type="hidden" name="submitted" value="true">
	<table>
	<tr>
		<td>Email:</td>
		<td>
		<input type="text" name="email"
							value="<cfoutput>#FORM.Email#</cfoutput>" size="30">
		</td>
	</tr>
	<tr>
		<td colspan="2" align="right">
			<input type="submit" value="Remind Me">
		</td>
	</tr>
	</table>
	</form>
<cfelse>
	<!---
	Write code that looks for the user's password
	in the Users table. If the password is found, 
	email it to the user.
	If the password is not found (i.e, there is
	no such email address in the Users table),
	return a message saying no such user exists.
	--->
</cfif>

</body>
</html>

Solution:

Email/Solutions/PasswordReminder.cfm
---- C O D E   O M I T T E D ----
<cfelse>
	<cfquery name="pwcheck" datasource="#APPLICATION.datasource#">
		SELECT password
		FROM Users
		WHERE email='#FORM.email#'
	</cfquery>
	<cfif pwcheck.RecordCount EQ 1>
		<cfmail to="#FORM.email#" from="info@RunnersHome.com" subject="Password Reminder">
			<cfmailpart type="html">
			Your password is: <b>#pwcheck.password#</b>
			</cfmailpart>
			<cfmailpart type="text">
			Your password is: #pwcheck.password#
			</cfmailpart>			
		</cfmail>
		<p>Your password has been sent.</p>
	<cfelse>
		<p>Email address not found.</p>
		<p><a href="Register.cfm">Register now.</a></p>
	</cfif>
---- C O D E   O M I T T E D ----

Next