ASP,tutorial,will,share,good,site,function,wi
Quick Search for:  in language:    
ASP,tutorial,will,share,good,site,function,wi
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
ASP/ VbScript Stats

 Code: 144,429 lines
 Jobs: 168 postings

 
Sponsored by:

 

You are in:

 
Login


 

 


Latest Code Ticker for ASP/ VbScript.
Click here to see a screenshot of this code!Ping in ASP
By Michele_Garneri on 10/28

(Screen Shot)

Embed Real Player Object
By Ziae Mousavi m. on 10/27


Set Country --> Combobox
By Hohl David on 10/27


Client Side Sorting of records
By Ravi Rajan on 10/26


Recordset paging with images
By Ravi Rajan on 10/26


Click here to see a screenshot of this code!Online photo catalogue VBScript 2.1
By Ivan Loire on 10/26

(Screen Shot)

GPS 1.4 WYSIWYG
By Guo Xu on 10/25


Click here to see a screenshot of this code!A Network Monitor tool from ActivXperts Software Inc.
By Freddy Hofstadt on 10/25

(Screen Shot)

Socket samples based on Winsock, TCP/IP and client/server communication
By Ronny Bright on 10/25


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!





Affiliate Sites



 
 
   

Sending emails from asp by reading them from text files

Print
Email
 

Submitted on: 4/1/2001 8:40:19 PM
By: Cyquester Technologies</a><img src=http://www.cyquester.com/logo_small.gif>  
Level: Intermediate
User Rating: By 6 Users
Compatibility:ASP (Active Server Pages)

Users have accessed this article 14023 times.
 

(About the author)
 
     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.

This article has accompanying files
 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
1) You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
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.

The requirements for this tutorial are simple:

  • IIS or PWS
  • ASP Email(available for free at ASPemail.com)

    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:

    strMail="Welcome to Bakers.biz," & username & Chr(10) 'Chr(10) is used to skip to the new line
    strMail= strMail & "Your order has been successfully queued!" & Chr(10)
    strMail=strMail & "Thank you for your business!"

    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.

    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.

    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:

  • ForgotPass1.asp- The file which will consist of basic a basic HTML form which would ask for the user's e-mail address.
  • ForgotPass2.asp- The file which will e-mails the user their username and password.
  • ForgotLogin.txt- Contain the text for the login information.
  • DB.MDB-A one-table database which will hold the username, password, and the e-mail address of members.

    ForgotLogin.txt

    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:

    Hello,

    You had requested your login information. Well, here it goes:

    Username:{username}
    Password:{password}

    Sincerely,

    The Bakers Team

    I am sure you must be wondering what the purpose of {username} and {password} in the text file. I will tell you about that in a moment... so hang on!

    DB.MDB

    You can tell by its name that its an Access file. DB.MDB will have one table called tbl_Users. Below is the basic structure of tbl_Users:

    NOTE: You can download all files(including this) that are part of this project as a Zip by clicking here.



    ForgotPass1.asp

    ForgotPass1.asp is simply going to have an HTML form asking for the e-mail address of the user(as displayed below).



    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.

    ForgotPass2.asp

    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.

    <%
     Email=Request.Form("Email")
        
     if Len(Email) > 0 Then 
        
     	strConnect="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="& Server.MapPath("DB.mdb")&";"
        
     	SQL="SELECT * FROM tbl_Users WHERE tbl_Users.Email='" & email & "'"
        
     	Dim objForgotLogin
        
     	Set objForgotLogin = Server.CreateObject("Adodb.Recordset") 'Used for pulling main categories from DB
        
     	objForgotLogin.Open SQL,strConnect
        
     		if objForgotLogin.EOF = False Then
        
     			'------------------Code For opening ForgotLogin.txt------------------------
        
     			Dim objFSO, objTextFile
        
     			Set objFSO = CreateObject("Scripting.FileSystemObject")
        
     			Set objTextFile = objFSO.OpenTextFile(Server.MapPath("ForgotLogin.txt"))
        
     			Do While Not objTextFile.AtEndOfStream
        
     			strTemp = strTemp & objTextFile.ReadLine & Chr(10)
        
     			Loop
        
     	
        
     			'------------------End of code For opening ForgotLogin.txt
        
     		
        
     			strTemp=Replace(strTemp,"{username}",objForgotLogin("Username"))
        
     			strTemp=Replace(strTemp,"{password}",objForgotLogin("Password"))
        
     			SendMail "Zaid","zaid@designMD.com",strTemp,objForgotLogin("email"),"Your login information is enclosed!"
        
     			Response.write "<FONT face='Verdana' size=2>Your login information has been e-mailed To you!"
        
     			Set objFSO=Nothing
        
     			Set objTextFile=Nothing
        
     		Else
        
     			Response.Write "<FONT face='Verdana' size=2 color='red'>Your e-mail address could Not be found!"
        
     		End if
        
     		
        
     	Set objForgotLogin=Nothing
        
     	
        
     Else
        
     	Response.Write "<FONT face='Verdana' size=2>Please enter an e-mail address</FONT>"
        
     End if
        
     'The function for sending the email using ASPEmail
        
     Function SendMail (FromName,FromEmail,MailContent,MailingAddress,Subject)
        
     Set Mail = Server.CreateObject("Persits.MailSender")
        
     Mail.Host = "smtp.amexol.net" ' Specify a valid SMTP server
        
     
        
     Mail.From = FromEmail ' Specify sender's address
        
     Mail.FromName = FromName' Specify sender's name
        
     Mail.AddBcc MailingAddress
        
     
        
     Mail.Subject = subject
        
     Mail.Body = MailContent
        
     
        
     mail.Send 
        
     Set Mail=Nothing
        
     End Function
        
     %>
        


    Let us start off by quickly getting thru some of the basics. The first if 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.

    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 SELECT statement like the one shown below:

    SQL="SELECT * FROM tbl_Users WHERE tbl_Users.Email='" & email & "'"


    Next, we use a recordset object to execute the above SQL query. Notice that strConnect holds the connection string for connecting to DB.MDB.

    Dim objForgotLogin
    Set objForgotLogin = Server.CreateObject("Adodb.Recordset") 'Used for pulling main categories from DB
    objForgotLogin.Open SQL,strConnect


    Once we have opened the SQL statement, we have to analyze the results. We start out by checking to see whether the EOF 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.

    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.

    First, look at the code below:

    Dim objFSO, objTextFile

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    Set objTextFile = objFSO.OpenTextFile(Server.MapPath("ForgotLogin.txt"))

    Do While Not objTextFile.AtEndOfStream

    strTemp = strTemp & objTextFile.ReadLine & Chr(10)

    Loop



    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 strTemp. Do you remember that in the text file(forgotlogin.txt), we had text {username} and {password}? Well they were created as the hotspots which we can replace with the real username and password. We do this by using the simple Replace 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.

    strTemp=Replace(strTemp,"{username}",objForgotLogin("Username"))
    strTemp=Replace(strTemp,"{password}",objForgotLogin("Password"))


    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:

    SendMail "Zaid","zaid@designMD.com",strTemp,objForgotLogin("email"),"Your login information is enclosed!"


    Notice that we specify strTemp as the content of the e-mail. Also, we get the user's e-mail address from the recordset(objForgotLogin("email")).

    That is really all there is to this example.

    NOTE:SendMail 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.



    Setting up the applciation

    Setting up this example is very easy. Just follow the simple steps:

    1. Download the example.
    2. Unzip the example.
    3. Copy the folder named "Example" and paste it into the wwwroot directory.

    NOTE: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.

    If you continue to have problems, feel free to contact me at zaid@designMD.com.
  • winzip iconDownload article

    Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzipto decompress it.

    Virus note:All files are scanned once-a-day by Planet Source Code for viruses,but new viruses come out every day, so no prevention program can catch 100% of them.

    FOR YOUR OWN SAFETY, PLEASE:
    1)Re-scan downloaded files using your personal virus checker before using it.
    2)NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

    If you don't have a virus scanner, you can get one at many places on the net including:McAfee.com

     
    Terms of Agreement:   
    By using this article, you agree to the following terms...   
    1) You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
    2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
    3) You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
    4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.


    Other 2 submission(s) by this author

     

     
    Report Bad Submission
    Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
    Reason:
     
    Your Vote!

    What do you think of this article(in the Intermediate category)?
    (The article with your highest vote will win this month's coding contest!)
    Excellent  Good  Average  Below Average  Poor See Voting Log
     
    Other User Comments
    4/1/2001 8:41:22 PM:Zaid Farooqui
    Please vote for me if you like this or if you find it useful. Thanks.
    Keep the Planet clean! If this comment was disrespectful, please report it:
    Reason:

     
    4/3/2001 7:37:41 PM:Luis Guerreiro
    Can't dowload the zip
    Keep the Planet clean! If this comment was disrespectful, please report it:
    Reason:

     
    4/3/2001 8:04:26 PM:zaid farooqui
    Hii... i am sorry i forgot to post it when i first submitted the article... but here you go. http://www.planet-source-code.com/uplo ad/ftp/Sending%20em17875432001.zip -- Zaid
    Keep the Planet clean! If this comment was disrespectful, please report it:
    Reason:

     
    4/3/2001 8:09:53 PM:Luis Guerreiro
    Sorry I found the zip
    Keep the Planet clean! If this comment was disrespectful, please report it:
    Reason:

     
    4/14/2001 9:23:01 PM:Kaustav Acharya
    Not bad! Pretty useful! BTW, looks like you're in front of me in the coding contest. Congrats! Always happy to learn from a fellow coder! :)
    Keep the Planet clean! If this comment was disrespectful, please report it:
    Reason:

     
    7/14/2001 7:43:48 AM:Farrukh
    Hi Zaid !! This is now, One of my Beautiful Codes that i have in my Computer. Hay Form ur Pic U look quite younger although I like your work.... (y) Keep up the Good work. Happy Programming..... Be :) and always ;)
    Keep the Planet clean! If this comment was disrespectful, please report it:
    Reason:

     
    9/19/2002 11:12:32 PM:
    alamak can put all the source code from beginning to end? etc from <html> to </html> instead of having to download cause some people don't have winzip thanks
    Keep the Planet clean! If this comment was disrespectful, please report it:
    Reason:

     
    9/19/2002 11:40:47 PM:
    cannot add leh page not displayed
    Keep the Planet clean! If this comment was disrespectful, please report it:
    Reason:

     
    Add Your Feedback!
    Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

    NOTICE: The author of this article has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

    For feedback not related to this particular article, please click here.
     
    Name:
    Comment:

     

    Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | ASP/ VbScript Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

    Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.