Sending email with ASP 3.0 and CDONTS (Rated 4.14)Description:
This script provides the capacity to send e-mail from a web page, via an HTML form. The form posts to an ASP script which creates the required objects, sends the e-mail and thanks the end user for their comments. Code starts here
<%@ Language=VBScript %>
<%
Option Explicit
%>
<%
' -----------------------------------------------------
' CDONTS Email send script
' © http://www.designplace.org/
' Comments must remain intact for re-use of this code
' This page contains the HTML form and select case statements...
' ...which show any errors.
' -----------------------------------------------------
' -- declare our variables --
dim strErrText, strAction
' -- place the action parameter into the strAction variable --
strAction = Request.QueryString("action")
' -- check the querystring for err=value parameters and show the required error --
Select Case strAction
Case "err1"
strErrText = "<span style='color: red; font-weight: bold;'>Error:
No name input.</span>"
Case "err2"
strErrText = strErrText & "<span style='color: red; font-weight:
bold;'>Error: No email input.</span>"
Case "err3"
strErrText = strErrText & "<span style='color: red; font-weight:
bold;'>Error: No age input.</span>"
Case "err4"
strErrText = strErrText & "<span style='color: red; font-weight:
bold;'>Error: No gender input.</span>"
Case "err5"
strErrText = strErrText & "<span style='color: red; font-weight:
bold;'>Error: No message input.</span>"
End Select
%>
<html>
<head>
<title>Contact us!</title>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<%
' -- if our querystring contains an action= parameter, it means theres an error and it will be shown --
if strAction > "" then
response.write strErrText
end if
%>
<p>Please fill out the form and click submit. All fields are required.</p>
<form name="form1" method="post" action="send_it.asp">
<p>Name: <br>
<input name="name" type="text" size="30"
maxlength="30">
</p>
<p>Email address: <br>
<input name="email" type="text" size="30"
maxlength="40">
</p>
<p>Age: <br>
<input name="age" type="text" size="5" maxlength="2">
</p>
<p>Gender: <br>
<select name="gender">
<option selected value="">Choose....</option>
<option value="male">Male</option>
<option value="female">Female</option>
</select>
</p>
<p>Message: <br>
<textarea name="message" cols="30" rows="5"></textarea>
</p>
<input type="submit" value="submit">
<input type="reset" value="reset">
</form>
</body>
</html>
The next ASP page is called send_it.asp, which is the value of "action", within the above form. Again, either copy the code into a text editor, or download the script files
<%@ Language=VBScript %>
<%
Option Explicit
%>
<%
' -----------------------------------------------------
' CDONTS Email send script
' © http://www.designplace.org/
' Comments must remain intact for re-use of this code
' -----------------------------------------------------
' -- declare our variables --
dim strName, strEmail, strMessage, strAge, optGender
' -- attach our variables to the form elements held within the forms collection --
strName = Request.Form("name") ' holds inputted name
strEmail = Request.Form("email") ' holds inputted email address
strMessage = Request.Form("message") ' holds inputted message
strAge = Request.Form("age") ' holds inputted age
optGender = Request.Form("gender").Item ' drop down list selection
' -- check all fields for empty values --
' -- remove and add new as required --
if strName = "" then
Response.Redirect "contact.asp?action=err1"
else if strEmail = "" then
Response.Redirect "contact.asp?action=err2"
else if strMessage = "" then
Response.Redirect "contact.asp?action=err5"
else if strAge = "" then
Response.Redirect "contact.asp?action=err3"
else if optGender = "" then
Response.Redirect "contact.asp?action=err4"
end if
end if
end if
end if
end if
' -- begin email send process --
' -- declare our main variable --
dim objMail
' -- set our email object variable as the required object type --
Set objMail = CreateObject("CDONTS.NewMail")
' -- email variables --
' -- change the to field to the email address you want to send the email to.
' -- change the subject and bodyformat if necessary
objMail.To = "user@domain.com"
objMail.From = Trim(strEmail)
objMail.Subject = "Feedback"
objMail.BodyFormat = "0" ' -- HTML format
objMail.Body = "Name: " & Trim(strName) & vbCrLf _
& "Gender: " & optGender & vbCrLf _
& "Age: " & Trim(strAge) & vbCrLf _
& "E-Mail Address: " & Trim(strEmail) & vbCrLf _
& "Message: " & Trim(strMessage)
' -- send the email --
objMail.Send
' -- clean up object
Set objMail = Nothing
' -- execute confirmation page
Response.Redirect "thanks.html"
%>
Our final page, is the confirmation page, which will thank the user for their input and confirm that it has been sent. I've called this page thanks.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!-- cdonts email send script, © http://www.designplace.org/ -->
<html>
<head>
<title>Confirmation</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<h1>Feedback sent</h1>
<p>Thanks for filling out the form!</p>
<p>Your message has been sent.</p>
<p><em><font size="2">Webmaster at http://domain.com</font></em></p>
</body>
</html>
Last Edited: 2003-02-12 11:34:22
Submitted by matt on 12-02-2003 22:29 |