Emailing Form Data with ASP
Recently a client asked me to write a CGI program to handle the form output at his site. As usual, he wanted the form content, entered by the visitor, to be sent to him in a readable format.
As you have read above, my knowledge of Perl is not exactly enough to enable me to write serious CGI routines. I was about to refer him to a friend of mine, when I noticed (I haven’t developed his site) that his site has been developed in ASP (no programming, just HTML pages saved as something.asp). Voila! Writing a form handling routine in ASP is cake walk. Here is how we do it.
We’ll use the CDO (Collaborative Data Object) email object that comes with NT and other compliant systems. Don’t get much bothered. You can always call up your web host and find out what email object is being used. The methodology, in ASP, is almost the same.
There are two methods actually. Preferred by advanced developers is the one in which we have the form and the processing code (the one that is mentioned with <form action="">) in the same file.
Suppose we have a file named formthings.asp. I leave the HTML things to your capable intellect, and get my hands dirty straightaway with ASP. At the onset of the ASP code (that we are going to write in VBScript), we check how the file is being called.
if Request.ServerVariables("REQUEST_METHOD")="POST" then
do the coding…..
simply display the form, as if the page has been loaded for the first time.
This method is a bit complicated for a newbie, as there could be too many if_then_else_end_if nests. So what we’re going to do is, we are going to divide the work in two files. The first one simply contains a form that executes the file that emails the form content to a specified email address.
Let the first file be form.asp that has the following form:
<form method="post" action="sendemail.asp">
<input type="text" size="15" name="name" /><br />
<input type="text" size="15" name="email" /><br />
<textarea row="5" col="15"
<input type="submit" value="Submit" /> <input type="reset" value="Reset" />
You can see that this form calls "sendemail.asp", which contains the following code:
<% Dim sName, sEmail, sMessage, oSendMailer
‘We store the form values in the local variables so that the server is not taxed if we have to access the values again and again.
‘This creates a new variable that inherits all the features of CDONTS.
‘Now we supply the required information to this variable.
‘Remember sEmail is the visitor’s email.
oSendMailer.Subject="Visitor’s message from the web site."
‘sMessage contains the message entered by the visitor.
if oSendMailer.Send then
‘Keep in mind in order to use response.redirect,
‘you have to add
‘<% response.buffer=true %> just below
‘<%@ language=vbscript %>
Error could occur if you supply wrong values, or if there is a problem in the server.
So this is how you can email to you the form content from your web site.