Sending emails from asp by reading them from text files
In this tutorial, I will share a good site function with you. I will show a good method which can be used to send emails to users. The tutorial will focus on sending e-mails to users(using ASP Email) by opening text files in which e-mail text is stored. At the end, I will present a good example of everything dicussed by building a "Forgot Login" application.
AI
Riepilogo AI: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.
Codice sorgente
<HTML>
<font size=2 face=verdana>
Today, most sites send mails to users dynamically. This can be to alert users of a new post or send a user their login information. In this tutorial, I will share with you one of the best ways of incorporating this kind of feature into your ASP site. We will start by discussing the method and at the end I will present an example to you. The example will send login information to a user. If you are an advanced user or someone who does not like to read thru, just download the example files and read the setup instruction at the bottom of this page.
<BR><BR>
The requirements for this tutorial are simple:
<BR><BR>
<li>IIS or PWS<BR>
<li>ASP Email(available for free at <a href="httP://www.ASPEmail.com" target="_blank">ASPemail.com</a>)
<BR><BR>
When we want our ASP pages to send an e-mail to a user, we have to have the e-mail content stored somewhere, usually in our ASP page. This is the method most people today use. They often store the e-mail message in a string like below:
<BR><BR>
<table width="100%" border=0 bgcolor="#EBEBEB">
<TR>
<TD>
<CODE>
<font size=2>
strMail="Welcome to Bakers.biz," & username & Chr(10) <font color=green>'Chr(10) is used to skip to the new line
</font>
<BR>
strMail= strMail & "Your order has been successfully queued!" & Chr(10)
<BR>
strMail=strMail & "Thank you for your business!"
</TD></TR>
</TABLE>
<BR>
Although this is the most common method, it does not mean that it is the best method. Why? Because of one simple reason: it makes it diffcult for you to edit the text and also, it makes your code even dirtier. As a programmer, I always try to include as much code as I can in the ASP file and keep the rest for the include files. In the above example it might not look that bad, but when you get down to business and start sending those 100-lines e-mail to your users using this method, it simply will make your code look dirtier and dirtier.
<BR><BR>
To fight this problem, I came up with a solution. I decided to use the FileSystemObject to open text files. Now be honest and you tell me, would you like to write an e-mail in an enviroment filled with complex ASP code or in a blank and white editor window of Notepad? Having the e-mail content in a text file can help things be more organized and makes changing the content much quicker and easier. In the example below, we will see how we can use this technique in a real-world scenerio.
<HR color=black size=1>
<BR>
The example we are going to discuss will ask for the user's e-mail address and then e-mail the user with their username and password. The content of the e-mail will be read from the text file(ForgotLogin.txt). The application will consist of four total files(including database files and etc.). The files include:
<BR><BR></B>
<Li><b>ForgotPass1.asp</b>- The file which will consist of basic a basic HTML form which would ask for the user's e-mail address.
<Li><B>ForgotPass2.asp</B>- The file which will e-mails the user their username and password.
<LI><B>ForgotLogin.txt</B>- Contain the text for the login information.
<LI><B>DB.MDB</B>-A one-table database which will hold the username, password, and the e-mail address of members.
<BR><BR>
<font size=4 color=green>
<B>ForgotLogin.txt</B>
<BR><BR>
</FONT>
<font face=verdana size=2>
As mentioned earlier, this file will contain the text message which will be e-mailed to the user. The content of this file should be:
<BR><BR>
<TABLE WIDTH="100%" bgcolor="#EBEBEB">
<TR>
<TD>
<code>
Hello,
<BR><BR>
You had requested your login information. Well, here it goes:
<BR><BR>
Username:{username}<BR>
Password:{password}<BR><BR>
Sincerely,
<BR><BR>
The Bakers Team
</TD>
</TR>
</TABLE>
<BR>I am sure you must be wondering what the purpose of <i>{username}</i> and <i>{password}</i> in the text file. I will tell you about that in a moment... so hang on!
<BR><BR>
<font size=4 color=green>
<B>DB.MDB</B>
<BR><BR>
</FONT>
<font face=verdana size=2>
You can tell by its name that its an Access file. DB.MDB will have one table called <code><font size=2><B>tbl_Users</B></font></Code><font size=2 face="verdana">. Below is the basic structure of tbl_Users:<BR><BR><B><font color="#ff9900">NOTE: </B>You can download all files(including this) that are part of this project as a Zip by clicking here.<BR><BR><img src="http://www11.ewebcity.com/q2261/pic1.gif"><BR><BR>
<font size=4 color=green>
<B>ForgotPass1.asp</B>
<BR><BR>
</FONT>
<font face=verdana size=2>
</font>
<font face=Verdana size=2 color=black>
ForgotPass1.asp is simply going to have an HTML form asking for the e-mail address of the user(as displayed below).
<BR><BR>
<img src="http://www11.ewebcity.com/q2261/pic2.gif">
<BR><BR>
Note that this file(if wanted) could be saved as .HTM but just for the sake of keeping things a ASP, I gave it a .ASP extension.
<BR><BR>
<font size=4 color=green>
<B>ForgotPass2.asp</B>
<BR><BR>
</FONT>
<font face=verdana size=2>
</font>
<font face=Verdana size=2 color=black>ForgotPass2.asp is where most of the work is done. This page is responsible for retreiving the user login information from the database and then emailing it to the user. Below is the complete source code for this page. We will discuss portion-by-portion next. Note that you can find a link to download the whole project at the end of this tutorial.<BR><BR>
<TABLE WIDTH="100%" bgcolor="#EBEBEB">
<TR>
<TD>
<CODE>
<SPAN style="BACKGROUND-COLOR: rgb(255,255,0)"><FONT color=black><FONT color=#000080><</FONT>%</FONT></SPAN>
</PRE><PRE><FONT size=2> Email=Request.Form("Email")
</PRE><PRE><FONT size=2> <FONT color=#000080>if</FONT> Len(Email) <FONT color=#000080>></FONT> 0 <FONT color=#000080>Then</FONT>
</PRE><PRE><FONT size=2> strConnect="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="& Server.MapPath("DB.mdb")&";"
</PRE><PRE><FONT size=2> SQL="SELECT * FROM tbl_Users WHERE tbl_Users.Email='" & email & "'"
</PRE><PRE><FONT size=2> <FONT color=#000080>Dim</FONT> objForgotLogin
</PRE><PRE><FONT size=2> <FONT color=#000080>Set</FONT> objForgotLogin = Server.CreateObject("Adodb.Recordset") 'Used for pulling main categories from DB
</PRE><PRE><FONT size=2> objForgotLogin.Open SQL,strConnect
</PRE><PRE><FONT size=2> <FONT color=#000080>if</FONT> objForgotLogin.EOF = <FONT color=#000080>False</FONT> <FONT color=#000080>Then</FONT>
</PRE><PRE><FONT size=2 color=green> '------------------Code For opening ForgotLogin.txt------------------------
</PRE><PRE><FONT size=2> </font></font><FONT color=#000080>Dim</FONT> objFSO, objTextFile
</PRE><PRE><FONT size=2> <FONT color=#000080>Set</FONT> objFSO = CreateObject("Scripting.FileSystemObject")
</PRE><PRE><FONT size=2> <FONT color=#000080>Set</FONT> objTextFile = objFSO.OpenTextFile(Server.MapPath("ForgotLogin.txt"))
</PRE><PRE><FONT size=2> <FONT color=#000080>Do</FONT> <FONT color=#000080>While</FONT> <FONT color=#000080>Not</FONT> objTextFile.AtEndOfStream
</PRE><PRE><FONT size=2> strTemp = strTemp & objTextFile.ReadLine & Chr(10)
</PRE><PRE><FONT size=2> <FONT color=#000080>Loop</FONT>
</PRE><PRE><FONT size=2>
</PRE><PRE><FONT size=2> <font color="green">'------------------End of code For opening ForgotLogin.txt
</PRE><PRE><FONT size=2> </font> </font>
</PRE><PRE><FONT size=2> strTemp=Replace(strTemp,"{username}",objForgotLogin("Username"))
</PRE><PRE><FONT size=2> strTemp=Replace(strTemp,"{password}",objForgotLogin("Password"))
</PRE><PRE><FONT size=2> SendMail "Zaid","zaid@designMD.com",strTemp,objForgotLogin("email"),"Your login information is enclosed!"
</PRE><PRE><FONT size=2> Response.write "<FONT color=#000080><</FONT><SPAN style="COLOR: #800000">FONT</SPAN> face='Verdana' size=2<FONT color=#000080>></FONT>Your login information has been e-mailed <FONT color=#000080>To</FONT> you!"
</PRE><PRE><FONT size=2> <FONT color=#000080>Set</FONT> objFSO=Nothing
</PRE><PRE><FONT size=2> <FONT color=#000080>Set</FONT> objTextFile=Nothing
</PRE><PRE><FONT size=2> <FONT color=#000080>Else</FONT>
</PRE><PRE><FONT size=2> Response.Write "<FONT color=#000080><</FONT><SPAN style="COLOR: #800000">FONT</SPAN> face='Verdana' size=2 color='red'<FONT color=#000080>></FONT>Your e-mail address could <FONT color=#000080>Not</FONT> be found!"
</PRE><PRE><FONT size=2> <FONT color=#000080>End</FONT> <FONT color=#000080>if</FONT>
</PRE><PRE><FONT size=2>
</PRE><PRE><FONT size=2> <FONT color=#000080>Set</FONT> objForgotLogin=Nothing
</PRE><PRE><FONT size=2>
</PRE><PRE><FONT size=2> Else
</PRE><PRE><FONT size=2> Response.Write "<FONT color=#000080><</FONT><SPAN style="COLOR: #800000">FONT</SPAN> face='Verdana' size=2<FONT color=#000080>></FONT>Please enter an e-mail address<FONT color=#000080><</FONT>/<SPAN style="COLOR: #800000">FONT</SPAN><FONT color=#000080>></FONT>"
</PRE><PRE><FONT size=2> <FONT color=#000080>End</FONT> <FONT color=#000080>if</FONT>
</PRE><PRE><FONT size=2> <FONT color=#186125>'The function for sending the email using ASPEmail
</FONT></PRE><PRE><FONT size=2> <FONT color=#000080>Function</FONT> SendMail (FromName,FromEmail,MailContent,MailingAddress,Subject)
</PRE><PRE><FONT size=2> <FONT color=#000080>Set</FONT> Mail = Server.CreateObject("Persits.MailSender")
</PRE><PRE><FONT size=2> Mail.Host = "smtp.amexol.net" ' Specify a valid SMTP server
</PRE><PRE><FONT size=2>
</PRE><PRE><FONT size=2> Mail.From = FromEmail ' Specify sender's address
</PRE><PRE><FONT size=2> Mail.FromName = FromName' Specify sender's name
</PRE><PRE><FONT size=2> Mail.AddBcc MailingAddress
</PRE><PRE><FONT size=2>
</PRE><PRE><FONT size=2> Mail.Subject = subject
</PRE><PRE><FONT size=2> Mail.Body = MailContent
</PRE><PRE><FONT size=2>
</PRE><PRE><FONT size=2> mail.Send
</PRE><PRE><FONT size=2> <FONT color=#000080>Set</FONT> Mail=Nothing
</PRE><PRE><FONT size=2> <FONT color=#000080>End</FONT> <FONT color=#000080>Function</FONT>
</PRE><PRE><FONT size=2> <SPAN style="BACKGROUND-COLOR: rgb(255,255,0)"><FONT color=black>%<FONT color=#000080>></FONT></FONT></SPAN>
</PRE>
</TD>
</TR>
</TABLE>
<BR><BR>
<font size=2 face='Verdana'> Let us start off by quickly getting thru some of the basics. The first <i>if</i> statement checks to make sure that an e-mail address was atleast entered. If an e-mail address is not entered, an error message is displayed.
<BR>
<BR>
If an e-mail address was entered, we have to check to make sure it exist in the database. For this, we have to build the <CODE><font size=2>SELECT</font></CODE> statement like the one shown below:<BR><BR>
<TABLE border=0 bgcolor="#EBEBEB">
<TR>
<TD><CODE>
SQL="SELECT * FROM tbl_Users WHERE tbl_Users.Email='" & email & "'"
</CODE>
</TR>
</TD>
</TABLE>
<BR>
<BR>
Next, we use a recordset object to execute the above SQL query. Notice that <CODE><font size=2>strConnect</font></Code> holds the connection string for connecting to DB.MDB.<BR><BR>
<TABLE bgcolor="#EBEBEB" border=0>
<TR>
<TD>
<code>
Dim objForgotLogin
<BR>
Set objForgotLogin = Server.CreateObject("Adodb.Recordset") 'Used for pulling main categories from DB<BR>
objForgotLogin.Open SQL,strConnect
</TD>
</TR>
</TABLE>
<BR><BR>
Once we have opened the SQL statement, we have to analyze the results. We start out by checking to see whether the <CODE>EOF</CODE> property of the recordset is true or false. If it is true, then it means that the user does not exist in the database and therefore displays an error. If its false, it means the user does exist and the application therefore proceeds.<BR><BR>
Once it has been made sure that the user exists, we can send the user their username and password. Now comes the part you must be waiting for the most: integrating the content of the text file with the e-mail message to be sent. Well, here is how we do it. <BR><BR>
First, look at the code below:
<BR><BR>
<TABLE bgcolor="#EBEBEB" border=0 width="100%">
<TR>
<TD>
<code>
Dim objFSO, objTextFile<BR><BR>
Set objFSO = CreateObject("Scripting.FileSystemObject")<BR><BR>
Set objTextFile = objFSO.OpenTextFile(Server.MapPath("ForgotLogin.txt"))<BR><BR>
Do While Not objTextFile.AtEndOfStream<BR><BR>
strTemp = strTemp & objTextFile.ReadLine & Chr(10)<BR><BR>
Loop<BR><BR>
</CODE>
</TD>
</TR>
</TABLE>
<BR><BR>
All this peice of code does is that it opens the text file which holds the contents of the e-mail and saves the file in a temporary string called <CODE><font size=2>strTemp</CODE>. Do you remember that in the text file(forgotlogin.txt), we had text <CODE><Font size=2>{username} and {password}</Code>? Well they were created as the hotspots which we can replace with the real username and password. We do this by using the simple <CODE><font size=2>Replace</CODE> function of ASP/VBScript. What the code below does is that it first finds the word {username} in strTemp and then replaces it with the username the SQL query returned. The same is done for replacing the {password} with the "real" password.
<BR><BR>
<TABLE bgcolor="#EBEBEB" border=0 width="100%">
<TR>
<TD>
<code>
strTemp=Replace(strTemp,"{username}",objForgotLogin("Username"))
<BR>
strTemp=Replace(strTemp,"{password}",objForgotLogin("Password"))
</CODE>
</TD>
</TR>
</TABLE>
<BR><BR>
Well, belive it or not but once we have done this, all we have to do next is to send the e-mail to the user. We do this using the following code:<BR><BR>
<TABLE WIDTH="100%" border=0 bgcolor="#EBEBEB">
<TR><TD><CODE>
SendMail "Zaid","zaid@designMD.com",strTemp,objForgotLogin("email"),"Your login information is enclosed!"
</CODE>
</TR></TD></TABLE>
<BR><BR>
Notice that we specify <CODE><font size=2>strTemp</CODE> as the content of the e-mail. Also, we get the user's e-mail address from the recordset(<CODE><font size=2>objForgotLogin("email")</code>). <BR><BR>
That is really all there is to this example.
<BR>
<BR>
<font color="orange">
<B>NOTE:</B><CODE><font size=2>SendMail</CODE> is a function I quickly developed to keep the function for e-mailing the user out of the main application code. I wont get into the details of it as it is very self-explanatory. You can goto ASPemail.com if you want the details about ASPEmail.
<BR><BR><BR><B>
<FONT size=4 color=green font='verdana'>
<BR>
Setting up the applciation<BR><BR></B>
<font color=black size=2 face=verdana>Setting up this example is very easy. Just follow the simple steps:<BR><BR>
1. Download the example.<BR>
2. Unzip the example.<BR>
3. Copy the folder named "Example" and paste it into the <i>wwwroot</i> directory.<BR>
<BR>
<B>NOTE:</B>You will have to open the MS Access database and add your e-mail address into tbl_Users before you can test it. Also, this database is for MS Access 2000 and if you have any problems, just re-make the Access file with the fields described in the DB.MDB section at the beginning of the tutorial. Also, you will need to change the connection string in ForgotPass2.asp.
<BR><BR>
If you continue to have problems, feel free to contact me at zaid@designMD.com.
Commenti originali (3)
Recuperato da Wayback Machine