Mark Voss

A generic CDOSYS form-to-email script

A generic CDOSYS form-to-email script

A simple classic ASP form processing script which collects input from any form on your website and emails it to you using the CDOSYS ASP component. The user is then redirected to a page of your choice.

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 his 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">
  <fieldset>
    <label for="name">Your Name </label>
    <input type="text" id="name" name="name">
    <label for="company">Company/Organisation </label>
    <input type="text" id="company" name="company">
    <label for="email_address">E-Mail Address </label>
    <input type="text" id="email_address" name="email_address">
    <label for="phone_number">Phone number </label>
    <input type="text" id="phone_number" name="phone_number">
    <label for="message">Message </label> 
    <textarea id="message" name="message" cols="25" rows="10"></textarea>
    <input name="email_subject" type="hidden" value="Subject of email"> 
    <input name="redirect_to" type="hidden" value="thanks.htm"> 
    <input type="reset" name="Reset" value="Reset"> 
    <input name="send" type="submit" value="Submit">
  </fieldset>
</form>

Next, copy the code below into a new page named cdosys.asp and change the SMTP settings and the from, reply to and to email addresses in the script to your own.

<%
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 strSchemas, objCDOSYSMail, objCDOSYSCon

strSchemas = "http://schemas.microsoft.com/cdo/configuration/"

'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
  
  'Specify the authentication mechanism: cdoAnonymous = 0 cdoBasic = 1 cdoNTLM = 2
  .Fields(strSchemas & "smtpauthenticate") = 1
  
  'SMTP Server username for authentication (hosted email account at domain.com)
  .Fields(strSchemas & "sendusername") = "mail@domain.com"
  
  'SMTP Server password for the email account above 
  .Fields(strSchemas & "sendpassword") = "password"
  
  'Outgoing SMTP server
  .Fields(strSchemas & "smtpserver") = "smtp.domain.net"
  
  'SMTP port 465 for SSL, 587 for TLS
  .Fields(strSchemas & "smtpserverport") = 587
  
  'Send using SSL on port 465 (comment out for TLS on port 587)
  '.Fields(strSchemas & "smtpusessl") = true
  
  'CDO Port (1=localhost 2=network)
  .Fields(strSchemas & "sendusing") = 2
  
  'Timeout
  .Fields(strSchemas & "smtpconnectiontimeout") = 60
  .Fields.Update 

End With

'Update the CDOSYS Configuration
  Set objCDOSYSMail.Configuration = objCDOSYSCon

'Set and update email properties
With objCDOSYSMail
  
  'Importance of email: 0=Low, 1=Normal, 2=High
  .Fields("urn:schemas:httpmail:importance").Value = 1
  
  'Who the e-mail is from (hosted email account at domain.com)
  .From = form@domain.com

  'Set the reply to address to the form submitter's email address
  .ReplyTo = Trim(Request.Form("email_address"))
  
  'Who the e-mail is sent to (hosted email account at domain.com)
  .To = "mail@domain.com"

  '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")
%>

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).

Leave a Reply

Your e-mail address will not be published. Required fields are marked *