
This tutorial is designed to help you setup a simple contact form processing script that will send an email via the ASP CDO component.
Setting this up is not a difficult task, and all you need is the Collaborative Data Object (CDO) that ships with NT option pack 4, and Windows XP.
History: Before Windows 2000, there was a Component called CDONTS that shipped with the IIS SMTP service that you could use to send email, but that is no longer supporter. You need to use CDO technology if your web server is running on Windows 2000/XP.
The first thing we need to do is create an HTML web page that contains a form, and a few form fields to allow our users to specify what information they want in their email etc. The form will contain the following information:
For the target email address, we shall use a hidden field to that people do not see where the email is getting sent to. This is an anti-spam mechanism that stops harvester bot's from gathering email addresses from your web page and adding you to a spam list.
The HTML code below can be used for the basic form. A list of the fields follows it.
<form action="send-cdo-email.asp"
method="post" name="contactfrm" id="contactfrm">
<table width="359" border="0" cellpadding="3">
<tr valign="top">
<td width="99">Senders Emai:</td>
<td width="215"> <input name="txtEmailFrom"
type="text" id="txtEmailTo"></td>
</tr>
<tr valign="top">
<td>Subject:</td>
<td> <input name="txtSubject" type="text"
id="txtSubject"></td>
</tr>
<tr valign="top">
<td>Message:</td>
<td> <textarea name="txtMessage" id="txtMessage"></textarea></td>
</tr>
<tr valign="top">
<td> </td>
<td><div align="right">
<input name="hidEmailTo" type="hidden" id="hidEmailTo"
value="target-email@your-domain.com">
<input type="submit" name="Submit" value="Submit">
</div></td>
</tr>
</table>
</form>
In the code above, we layout a table to hold the form fields and make them look organised. As you can see from the forms action property, this form will sends it information to a file called 'send-cdo-email.asp' using the POST method.
The Form itself consists of 4 form fields. Two Text Fields, a Text Area, and a Hidden Field. details follow:
| Field Name | Field Type | Description |
|---|---|---|
| txtEmailFrom | text | This field will hold the senders email address.
It will be used in the 'from' field of the email. |
| txtSubject | text | This field will allow the sender to specify
the subject line for the email. |
| txtMessage | textarea | This text area will let the user enter their
message for the email. |
| hidEmailTo | Hidden | This hidden field should be set to the email address you wish all emails to be sent to. |
Once you have setup this form and saved it, we can start the mail processing script.
The first thing we need to do with the script is to create a new ASP page and save it as 'send-cdo-email.asp', or whatever filename you told the form in the previous step to point to.
After this we can code our Form Processing Script. The Basic code is listed below:
<%
Set myMail=CreateObject("CDO.Message")
myMail.Subject=request.form("txtSubject")
myMail.From=request.form("txtEmailFrom")
myMail.To=request.form("hidEmailTo")
myMail.TextBody=request.form("txtMessage")
myMail.Send
set myMail=nothing
response.redirect("thanks.html")
%>
In the above code, the first thing that happens is an instance of the CDO.Message component is created in an variable called myMail. From this object, we can setup our email and call the required functions in order to send the message.
In the next 4 lines, we retrieve the data submitted from our form by using the form("feldname") function of the request object, and store them in the related properties of the CDO component.
NOTE: when dealing with variable and field names returned from the request object, the rules for CaSe SeNsItIvItY need to be followed, or no values will be returned. What this means is that the name of the variable you try and request MUST match EXACTLY, the name of the form field you are trying to retrieve.
In the next line, we call the Send() function of the myMail object to send the email using the default SMTP server of the web server, (if one is configured). If none, is configured, the email will not send and you will receive an error. In this instance, please refer to the next section for 'Sending Email Via a Remote Server'.
The next line clears the CDO object from memory.
The final line redirects the browser to another html page that informs the user that their email has been sent. You can create this file yourself and use whatever name.
If you encountered problems sending email in the previous example, its possibly because your web server does not have an SMTP server configured. Contact your Web Hoster to find out if this is the case.
If you do not have an SMTP server configured, you can tell the CDO component to use a REMOTE SERVER, that is, another server elsewhere, to send the email out. For this you will need the smtp address of an email server.
You may also need an email account on that server with a user name and password.
If you have an email account with your ISP, you can use this account and mail server to send the emails posted from your web site. Please check this is ok with your ISP before hand as you don't want to get into trouble.
Configuring CDO to use a remote server is pretty straight forward. Simple add the following code before the myMail.Send line in the previous script. You will need to change the Items in red to match your chosen mail servers settings.
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.server.com"
'Server port
myMail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport")
_
=25
'Authentication Info (ignore if no authentication required by mail server)
' Specify the authentication mechanism
' to use.
myMail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate")=
_ cdoBasic
'The username for authenticating to an SMTP server
using
'basic (clear-text) authentication
myMail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusername")=
_ "YourLogin@YourDomain.com"
'The password used to authenticate to an SMTP
server
'using authentication
myMail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendpassword")
=
_ "Password"
myMail.Configuration.Fields.Update
If you have any problems with this script, please join the forums and post a message requesting assistance.
Tutorial by Justin Kercher
2005.
This document may not be copied for use on another site.
Copyright© 2005.