One of my favorite features for IIS 4 is the SMTP service and a COM object that allows a programmer to send an e-mail message with just a few lines of ASP code. This SMTP service, along with a couple of ASP pages and a lookup text file, was my solution to the above problem.
I have created a few ASP pages that allow the programmer to gather information from the user and then send this information as an e-mail to the person who is responsible for the specific web.
There are 3 files that make up this little feedback application. They are as follows:
- feedback.asp - The Web Form that is filled out by the user submitting feedback
- mailfeedback.asp - The ASP page that assembles the e-mail message and sends it to its destination.
- maillookup.txt. - This is a text file that contains the web names and e-mail addresses that are going to use the feedback application.
Along with these files, there needs to be a hyperlink on the pages where you would like the end user to click to give feedback. All of these hyperlinks will be unique in that they will pass along the name of the web to the feedback.asp file. As an example take a look at the following URL.
http://yoursite.com/feedback.asp?web=marketing
By creating a hyperlink with the above URL you are pointing the user to the feedback.asp page and passing along a variable called web, which in this case tell feedback.asp that it is the marketing web. The code in feedback.asp uses the Request.QueryString("web") statement to read the information that is passed in this variable.
Next feedback.asp opens the maillookup.txt file to find a line of text that begins with marketing, since that was the web name that was passed to the feedback.asp file. If the line is found, then feedback.asp uses this information as the destination when sending the e-mail message. Multiple e-mail addresses can be used by separating them with a semicolon. If the web name is not found, then you can have the feedback go to a special e-mail address or to the address of the webmaster or server administrator. This is defined in the mailfeedback.asp file.
Below is the code from feedback.asp
<%
Response.Expires = 0
%>
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<body background="ccbackd.jpg">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title> IS Feedback </title>
<table border="0" width="924" cellpadding="0" cellspacing="0">
</head>
<body text="#000000" link="#008000" vlink="#408080" alink="#FF0000">
<p> </p>
<%
' Get the mail info from the URL line using request.querystring
' And get the referring URL by reading the HTTP_REFERER variable from the server.
strReferingURL = Request.ServerVariables("HTTP_REFERER")
strWebInfo = Request.QueryString("web")
strWebInfo = lcase(strWebInfo)
if strWebInfo = "" then
strWebInfo = "nothing"
End If
On Error Resume Next
Set objFSO = CreateObject("Scripting.FileSystemObject")
strFileName = "c:\inetpub\wwwroot\feedbackLookup\maillookup.txt"
set objFile = objFSO.OpenTextFile(strFileName)
Select Case Err.Number
Case 0
Do While Not objFile.AtEndOfStream
strLine = objFile.ReadLine
If lcase(left(strLine,len(strWebInfo))) = strWebInfo then
intPos = InStr(strLine, "=")
' Read after the = sign to the end of the line.
strMailTo = mid(strLine,intPos+1,len(strLine))
End If
Loop
' Close the file
objFile.Close
Case 50,53
' Can not read the file. Send the feedback to the administrator
strMailTo = "webmaster@yoursite.com"
End Select
set objFSO = Nothing
set objFile = Nothing
%>
<tr>
<td width="95"></td>
<td width="817"><font color="#008080" size="6"><p align="center">Welcome to the Inet
Feedback Web Page</font></td>
</tr>
<tr>
<td width="95"></td>
<td width="817"><font color="#008080" size="4"><p align="center">Please fill out the appropriate information and click
the button below.</p></font>
<p align="left"> </p>
<form action="mailfeedback.asp" method="POST">
<p><font color="#008080" size="3">Your Name:</font><input type="text" size="20" name="submitter_name"> </p>
<p><font color="#008080" size="3">Your Title:</font><input type="text" size="20" name="submitter_title"> </p>
<p><font color="#008080" size="3">Your Location:</font><input type="text" size="20" name="submitter_location"> </p>
<p><font color="#008080" size="3">Please enter your feedback in the box below. Thank You.</font></p>
<p><textarea name="text_feedback" rows="4" cols="51"></textarea></p>
<input type="hidden" name="mailto" value="<%= strMailTo%>">
<input type="hidden" name="strReferingURL" value="<%= strReferingURL%>">
<p><input type="submit" name="Submit" value="Send your Feedback"> </p>
</form>
<p> </p>
<p> </p>
</tr>
</table>
</body>
</html>
The ASP file first sets Respone.Expires = 0 so that the browser will not cache the page. The next section of ASP will set some variables. The first variable is HTTP_REFERER, which is a server side variable that contains the page that called this ASP page. Then Response.QueryString is used to read the web variable that was passed on the URL line. This web name is used when reading the maillookup.txt file.
The strFileName variable is used to define the full path and filename of the maillookup.txt file, so this needs to be changed to your specific path and directory. If there is an error reading the file (i.e., not found, etc) then the case 50,53 is used to set the mailto variable to the destination of the webmaster or an administrator so the feedback can go to some destination. The format of the maillookup.txt file is listed below.
Hr=jdoe@yoursite.com
Sales=don.sales@yoursite.com;jane.doe@yoursite.com
Marketing=youraddress@yoursite.com
WebMaster=webmaster@yourcompany.com
This file can be created with any text editor such as notepad and should contain each web that you want to use feedback on along with the e-mail address of the person who is responsible for the web. Each entry has to be on its own line with the webname=e-mail address format. Also be aware that when reading the text file the information is case sensitive.
After the ASP code you will see standard HTML code that is used to display the form that the user fills out. Fields can easily be added or deleted from this section of code.
It this example I am asking for Name, Title, Location and Comments. This can certainly be customized to fit specific needs.
Standard HTML input tags are used for the input. The mailto information and the referring URL are passed to the mailfeedback.asp file by using hidden input tags. This is one way to pass data from one page to another. Since we are using ASP, session variables can also be used.
After the user clicks the submit button the mailfeedback.asp page is executed. This page will read all of the text boxes that were on the HTML form including the mailto and referring URL information that are in hidden fields. This information is assembled in an e-mail message and sent to the destination address that was specified in the maillookup.txt file.
Here is the mailfeedback.asp code
<%
Response.Expires = 0
If Request.Form("text_feedback") = "" then
Response.redirect "feedback.asp"
End If
%>
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<body background="ccbackd.jpg">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title> IS Feedback </title>
<table border="0" width="924" cellpadding="0" cellspacing="0">
</head>
<body text="#000000" link="#008000" vlink="#408080" alink="#FF0000">
<p> </p>
<%
useragent = Request.ServerVariables("HTTP_USER_AGENT")
ipaddress = Request.ServerVariables("REMOTE_ADDR")
' The Response.Write commands are used for debugging purposes..
body = "This is an automated e-mail message. Please DO NOT Reply." & chr(13)
body = body & "This Feedback was submitted on: " & date & " at " & time & " by" & chr(13) & chr(13)
body = body & Request.Form("submitter_name") & chr(13)
body = body & Request.Form("submitter_title") & chr(13)
body = body & Request.Form("submitter_location") & chr(13)
body = body & "" & chr(13) & chr(13)
body = body & "Feedback - " & Request.Form("text_feedback") & chr(13)
body = body & chr(13)
body = body & "Refering URL: " & Request.Form("strReferingURL") & chr(13)
Set objMail = Server.CreateObject("CDONTS.NewMail")
objMail.From=" Web siteserver@yourcompany.com"
'Check to see if MailTo info is blank. If it is blank then send it
'to the webmaster.
If strMailTo = "" then
strMailTo = "webmaster@yourcompany.com"
End If
' Used for Debugging.
'Response.Write "Mail To is: " & strMailTo
objMail.To=Request.Form("Mailto")
objMail.Subject="Feedback Submitted By " & Request.Form("submitter_name")
objMail.Body=body
objMail.importance=1
objMail.Send
set objMail = Nothing
%>
<p align="center"><font color="#008080" size="5">Your feedback has been submitted.</font></p>
<p align="center"><font color="#FF0000" size="5">Thank you for your feedback.<p>
One of the first things to do is to make sure that the user is not coming to this page directly. I am doing this by looking at the text_feedback variable that is filled in on the feedback page. Again you can use a session variable to do this, or you can use another hidden HTML variable that would be on the feedback.asp page. Since users are coming to this page because they want to send some feedback, I am looking at the text_feedback variable. I use Response.Redirect to redirect them to the feedback.asp page if the text_feedback variable is blank.
Next, the body of the message is assembled, and then the CDONTS COM object is called. The To, From, Subject, Body and Importance information is set, and then objmail.Send is used to send the e-mail. Finally, an HTML message is displayed thanking the user for their feedback.