SearchSearch   MemberlistMembers   RegisterRegister   ProfileProfile   Log inLog in 

A Generic Form-to-Email Script Using CDOSYS (ASP)

 
Post new topic   Reply to topic    Open Hosting Internet Solutions Forum Index -> Articles and Tutorials -> Server Side Web Development
View previous topic :: View next topic  
Author Message
Mark Voss
Divine Being


Joined: 19 Jun 2002
Posts: 4946
Location: Cardiff, UK

PostPosted: Sun Feb 26, 2006 10:49 pm    Post subject: A Generic Form-to-Email Script Using CDOSYS (ASP) Reply with quote

A Generic Form-to-Email Script Using CDOSYS (ASP)

A simple ASP form processing script which collects input from any form on your website and emails it to you. 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 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.

Code:
<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

Code:
<%
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

Code:
.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

Code:
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.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Open Hosting Internet Solutions Forum Index -> Articles and Tutorials -> Server Side Web Development All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB 2.0.11 © 2001, 2002 phpBB Group
FAQClick here for help using the phpBB forum