21 Aug

Form-to-Email Script Using CDOSYS (ASP)

Form-to-Email Script Using CDOSYS (ASP)

First, build your HTML form using the usual text, textarea, checkbox, select and radio button fields.

The only required fields for the script to work are “email_address” where the user must enter their email address, the hidden “email_subject” field and the hidden “redirect_to” field which tells the script which page to go to once the email has been sent.

Below is a simple example of such a form.

<form method="post" action="cdosys.asp">
Email address: <input type="text" name="email_address" /><br />
Question 1: <input type="text" name="question_1" /><br />
Question 2: <input type="text" name="question_2" /><br />
Question 3: <input type="text" name="question_3" /><br />
<input name="email_subject" type="hidden" value="Subject of email" />
<input name="redirect_to" type="hidden" value="redirect_to.htm" />
<input type="reset" name="Reset" value="Reset" />
<input name="send" type="submit" value="Submit" />
</form>

Next, copy the code below, paste it into a new page and name it cdosys.asp 

<%
For Field = 1 to Request.Form.Count - 3
FieldName = Replace(Request.Form.Key(Field),"_"," ")
FieldValue = Request.Form.Item(Field)
Body = Body & FieldName & ": " & FieldValue & VbCrLf
Next
'Dimension variables
Dim objCDOSYSCon
'Create the e-mail server object
Set objCDOSYSMail = Server.CreateObject("CDO.Message")
Set objCDOSYSCon = Server.CreateObject ("CDO.Configuration")
'Set and update fields properties
With objCDOSYSCon
'Outgoing SMTP server
.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "SMTPSERVER"
.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
'CDO Port
.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
'Timeout
.Fields("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
.Fields.Update
End With
'Update the CDOSYS Configuration
Set objCDOSYSMail.Configuration = objCDOSYSCon
'Set and update email properties
With objCDOSYSMail
'0=Low, 1=Normal, 2=High
.Fields("urn:schemas:httpmail:importance").Value = 1
'Who the e-mail is from
.From = Request.Form("email_address")
'Who the e-mail is sent to
.To = "your_email_address@your-domain.co.uk"
'Who the e-mail is CC'd to
.Cc = ""
'The subject of the e-mail
.Subject = Request.Form("email_subject")
'Set the e-mail body format (HTMLBody=HTML TextBody=Plain)
.TextBody = Body
.Fields.Update
'Send the e-mail
.Send
End With
'Close the server mail object
Set objCDOSYSMail = Nothing
Set objCDOSYSCon = Nothing
'Rederect after sending email
Response.Redirect Request.Form("redirect_to")
%>

Change the email address on the line 

.To = "your_email_address@your-domain.co.uk"

to the email address you want the form output mailed to. 

The code is commented so that further modifications (such as changing the email priority, adding a CC email address etc.) can be made easily.

If you add more hidden fields to the form, you must place them after the visible fields and change the number at the end of the first line in cdosys.asp

For Field = 1 to Request.Form.Count - 3

The number should be the number of hidden fields in the form + 1 (in this case, 2 hidden fields + 1 = 3). 

All you have to do now is upload both the form page and the cdosys.asp script page to your webspace.

Share this