How To : Email Users their Forgotten Passwords

As the usage of internet is increasing every day we come across lot of registrations on each websites and it is very difficult to remember all the passwords which we used during the registration process.

Hence this program will be of very useful. I use the CDONTS component that comes with IIS to mail the password to the users. (For a good article on CDONTS, check out: Sending Email Using CDONTS.) A user who has forgotten his login information is directed to a page that asks for his email address. The page posts the entered email address to a second page which checks if the email address entered is in the database. If the email address is not there, the user will be prompted as such. If the email address is there, the script sends an email to the address entered with the username and password for that person in the message body. We assume that your user database has a username, password, and an email field and the database table is called USERS. Now that we know what we are doing lets see what goes on in the backend. 

The first page, forgot.asp is a basic form that gathers a user's email address and posts it to sendit.asp. 
Note : There is no error checking here, whether the user has entered correct email address or not, so I request you to take care of all the validations. 

<title>Forgot Password?</title></head>
If you forgot your login information, enter you email
address below and it will be sent to you.
<form method="POST" action="sendit.asp"> 
Your email address: 
<input type="text" name="email" size="20">
<input type="submit" value="Request information">

We looked at creating the form where users could enter their email address to have their username/password emailed to them. In this part we will be looking at SendIt.asp, which is what actually does the work of emailing the user his or her username and password. First you need to make the database connection and declare your variables. 

<% Option Explicit %>
Dim DATA_PATH, objDC, objRS, email, user, pass, sendmail
'Maps to database. Change to your database path.
' Create and establish data connection
Set objDC = Server.CreateObject("ADODB.Connection")
objDC.ConnectionTimeout = 15
objDC.CommandTimeout = 30
objDC.Open "DBQ=" & DATA_PATH & _
";Driver={Microsoft Access Driver (*.mdb)}; " & _
"DriverId=25;MaxBufferSize=8192;Threads=20;", _ 
"admin", "password"
Set objRS = Server.CreateObject("ADODB.Recordset")
'you may need to adjust this to suit your database
objRS.Open "SELECT * FROM USERS WHERE email = '" & _
email & "'", objDC, 0,

Next we check to see if the email address is actually in the database.

<title>Forgotten Password</title>
'checks if email address exists in the database 
'before sending a message.
if objrs.EOF then
<B><font color="red">
There is no account for <%=email%>.
<% Else %> 

Then we set create a new CDONTS mail and define all the variables that the CDONTS component needs. You will need to set the sendmail.from value. Remember, we assume that there are fields in your database for email, username and, password. 

'sets variables
email = request.form("email")
'chooses username and password from database that correspond
'to submitted email address.
user = objrs.Fields("username")
pass = objrs.Fields("password")
Set sendmail = Server.CreateObject("CDONTS.NewMail")
'put the webmaster address here
sendmail.From = ""
'The mail is sent to the address entered in the previous page.
sendmail.To = email
'Enter the subject of your mail here
sendmail.Subject = "The Login Information You Requested"
'This is the content of thr message.
sendmail.Body = "The login Information you requested." & _
vbCrlf & vbCrlf & "Username=" & user & _
vbCrlf & "Password=" & pass & vbCrlf 
'this sets mail priority.... 0=low 1=normal 2=high
sendmail.Importance = 2
sendmail.Send 'Send the email!

Finally, we can show the user that a mail has been sent, and then close all the variables.

Your login information has been mailed to <%=email%>.<br> 
You should receive it shortly. 
' Close Data Access Objects and free DB variables 
Set objRS = Nothing 
Set objDC = Nothing 
Set sendmail = Nothing 
<%end if%>

Well, this is all about mailing the user his password! Happy Programming!!!

Added on July 2, 2007 Comment