Friday, March 29, 2024

A New Forms Solution Using ASP


Part 2


Return to Part 1



First, let me add a little additional thought to the mix. I mentioned in the
introductory paragraph that email forms are frequently used to send
messages back to the webmaster and are sometimes used as a mechanism for
capturing data, bypassing the need for a local database. This is the point I’d
like to touch on again; it appears to have piqued the curiosity of more than a
few readers!

It is easy enough to imagine the kinds of data that can usefully be captured on
a form. "Tell me who you are", "tell me what you like", "tell me what I can do
for you", "tell me what you want from me"; these are all good examples. It is
very convenient to simply add this data to a local database, for whatever later
processing you have in mind, but it is not always that easy. Not every host will
make a database available to you, at least, not for free! If you could capture
the data another way, and get it back to your own computer where you could put
it into a database yourself, that might be a good workable solution.

If you format all the captured data into the body of an email and send it to
yourself, you could cut and paste the data either directly into a database
(depending on the capabilities of your database management system) or into a
text file for later import into your database (which almost every DBMS can do.)
Since you as the programmer are completely responsible for the format of your
email, you can determine how you need it to look. It’s also worth remembering
that when you capture your data in this way, your email box becomes a back-up
for your data!

This solution uses Microsoft’s Collaboration Data Objects (CDO) for NT Systems (CDONTS).
CDONTS is a library designed to enable SMTP (Simple Mail Transport Protocol —
email) messaging. It is usually installed on NT or W2K systems, but should it be
missing, it can be installed by adding the SMTP feature to the Internet
Information Services (IIS) on the server (your host will understand this – or at
least, they should!)

Here is the example:

<%@ Language=VBScript %>
<HTML>
<HEAD>
<%
Dim sEmailAddr, sMsg

On Error Resume Next

sEmailAddr = Request.Form("EmailAddress")
If Len(sEmailAddr) > 0 Then
If InStr(1,sEmailAddr,"@") > 0 Then
Call SendEmail
Else
sMsg = "Please enter a valid email address."
End If
End If
If Err.number <> 0 Then
sMsg = Err.Description
End If

Sub SendEmail()

Dim oMail

On Error Resume Next
Set oMail = Server.CreateObject("CDONTS.NewMail")
With oMail
.From = "Testing@TestDomain.com"
.To = sEmailAddr
.Subject = "Testing Website"
.Body = "This is only a test"
.Send
End With
Set oMail = Nothing

If Err.number <> 0 Then
sMsg = Err.Description
Err.Clear
End If

End Sub
%>
<Script Language="JavaScript">
<%If Len(sMsg) > 0 Then%>
window.alert("<%=sMsg%>");
<%End If%>
</Script>
</HEAD>
<BODY>

<form id="frmTest" name="frmTest" method="post" action="TestEmail.asp"> <P
align="center">
Enter your email address
<input id="EmailAddress" name="EmailAddress" type="text" size="30" maxlength="64"
value="<%=sEmailAddr%>">
<input id="btnSend" name="btnSend" type="submit" value="Send"> </P> </form>

<%If Len(sEmailAddr) > 0 And Len(sMsg) = 0 Then%>
<P align="center">
Your Email was sent successfully.
</P>
<%End If%>
</BODY>
</HTML>

That’s the whole thing! This one assumes it is in a file called "testemail.asp".
You can create a file with this name, copy it into a directory on your website
and try it out! If you choose to call the file something else, you will need to
change the action="TestEmail.asp" near the bottom to reflect the name you choose
(of course, it must end ".asp")

Around the middle, you see a line with :
.From = "Testing@TestDomain.com"
This is the address the email appears to be sent from. You can change this to ad
address suitable for your needs. It is also possible that your host’s email
service has some restrictions on the domain name that is used in this email
address. If you have doubt, or if you have problems, check with your hosting
company.

A full explanation of this code is far beyond the scope of this newsletter, but
if you read it carefully, you’ll probably get the idea of what it is doing. You
can get more information about ASP in our tutorials on
http://www.htmlgoodies.com 
Another way to exploit it is to first get it working on your site, then slowly
make changes to it, trying each change out to verify that it works, and slowly
evolving it into something you find useful.
 


Return to Part 1


Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Popular Articles

Featured