Classic ASP – How To’s

How to make a database connection to MySQL in ASP

Note: The same basic idea should work for other database types. The sConnection value would need to be changed.

<html>
    <head>
        <title>ASP - MySQL Connection</title>
    </head>
    <body>
        <center><p><strong>ASP - MySQL Connection</strong></p>
        <p>
        <%
            Dim sConnection, objConn , objRS
            sConnection = "DRIVER={MySQL ODBC 3.51 Driver}; SERVER={Enter MySQL server name here}; DATABASE={Enter database name here}; UID={Enter database username here};PASSWORD={Enter database password here}; OPTION=3"

            Set objConn = Server.CreateObject("ADODB.Connection")
            objConn.Open(sConnection)

            Set objRS = objConn.Execute("SELECT lastname, firstname FROM test ORDER By lastname")
            While Not objRS.EOF
                Response.Write objRS.Fields("firstname") &amp; " " &amp; 
                    objRS.Fields("lastname") &amp; "&lt;br&gt;"
                    objRS.MoveNext
            Wend

            objRS.Close
            Set objRS = Nothing
            objConn.Close
            Set objConn = Nothing
        %>
        </p>
        </center>
    </body>
</html>

How to create an email web form in ASP

First, we need to create the form. We’ll call this file “feedback.html”.

<html> 
    <head> 
        <title>Feedback Form</title> 
    </head> 
    <body> 
        <!-- Form Section --> 
        <!-- Email addresses should never be passed from the from section, 
             instead pass a variable that can be translated to an email address 
             in the Post Section. --> 
        <h1>Contact Us</h1> 
        <form action="process_feedback.asp" method="post"> 
            <p>Your Name: <input type="text" name="name" /></p> 
            <p>Your Email: <input type="text" name="email" /></p> 
            <p>Comments: <textarea name="comments"></textarea></p> 
            <p><input type="submit" value="Send Email"></p> 
        </form> 

    </body> 
</html>

Then we need to create the script to process the form. We’ll call this file process_feedback.asp. This name must match the form’s action attribute.

<% @language=VBScript %>
<%
    'Set HTMLFormatted to 1 for HTML email and 0 for plain text
    HTMLFormatted = 1

    'Dimension variables
    Dim myMail

    'Get a Message object
    Set myMail = CreateObject("CDO.Message")

    'Configure the message object for the remote server
    'Send using (remote) server and port 
    myMail.Configuration.Fields.Item _ 
      ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
    'Name or IP of remote SMTP server
    myMail.Configuration.Fields.Item _
      ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.purdue.edu"
    'Server port
    myMail.Configuration.Fields.Item _
      ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
    'Connection timeout
    myMail.Configuration.Fields _
      ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60

    'Update the configuration
    myMail.Configuration.Fields.Update

    'Setup the From, To, Subject, and Body of the message
    myMail.Subject = "Feedback"
    myMail.From = "<" & request.form("email") & "> " & request.form("name")
    myMail.To = "sample@purdue.edu"
    myMail.ReplyTo = request.form("email")

    Dim Body
    if HTMLFormatted = 1 then
        Body = "<h1>Message from " & request.form("name") _ 
            & " (" & request.form("email") & ")</h1>"
        Body = Body & "<b>Comments:</b><br />" & request.form("comments")
        myMail.HTMLBody = Body
    else
        Body = "Message from " & request.form("name") _ 
            & " (" & request.form("email") & ")" & vbNewline
        Body = Body & "Comments:" & vbNewline & request.form("comments")
        myMail.TextBody = Body
    end if

    'Now send the message
    myMail.Send

    'Finally, free the Message object
    Set myMail = nothing
%>

<html> 
    <head> 
        <title>Thank You!</title> 
    </head> 
    <body> 
        <!-- Reply to the user --> 
        <h1>Thank You!</h1> 
        <p>Your comments have been submitted.<br /> 
        Thank you for your input.</p> 
    </body> 
</html> 

Note: Email addresses should never be passed from the Form Section. If recipient email addresses vary, pass a variable that can be translated to an email address in the Post Section.

Why not put the email address in the Form Section?

Email addresses should never be passed from the Form Section. If recipient email addresses vary, pass a variable that can be translated to an email address in the Post Section. Spammers use robots to find email addresses in HTML code. Keeping email addresses out of the Form Section will make it harder for spammers to find your email address. Also, allowing an email address to be passed directly to the Post Section allows less scrupulous visitors to use your form to send email for unintended uses.

The POST Request

When you click on that submit button, what happens? The browser sends all the form fields to the server using a POST request. A POST simply means that all the submitted fields are sent in the body of the request. In a GET request, the submitted fields are appended to the URL e.g. process_feedback.asp?name=Jason. The POST method is preferable here because the comments field might exceed the browser’s URL length limit if a GET request was used.

Using POST, the field names and values are sent to the server in the body of the request. When the ASP script handles the request, it puts these values into the request object in the form array with the field names as the keys. The value of the name field, for example, could then be accessed using request.form('name').

Sending the Email

First we create the myMail object (variable) as an instance of the class CDO.Message. We then set the values needed to allow that object to connect to the remote mail server and set various header fields in the email. A  Body variable containing the body of the message is built and attached to the object and we call the myMail.send() method to send the actual email.