Using the CDONTS Component.

The CDONTS script is an example of using a script to input form information and then emailing it using ASP and CDONTS.

Dim toAddress, subject
toAddress = "support@easycgi.com"
subject = "Thanks for testing this great Easy CGI Form!"

Here we define two variables that will hold the subject of the email to be sent and the address that the email will be sent to.

if request.form("submit") <> "" Then
Since the code to send the email and output the form is in this script, we must check if the form is being submitted. If it is the email will be sent, if it is not then we will skip past the email code and output the form.

Set objCDO = Server.CreateObject("CDONTS.NewMail")
Sets the mail object to use, which is CDONTS.

objCDO.From = request.form("email")
Takes the email address that was submitted in the form by the user and sends the mail from that address. In order for the mail to be sent properly, this should be a valid address.

objCDO.To = toAddress
Adds the email address that the email will be sent to, taken from the variable that was defined above. You can add multiple recipients by separating the email addresses by semicolons. For example, you can specify three recipients by changing the definition for the toAddress variable to - toAddress = "support1@easycgi.com;support2@easycgi.com;support3@easycgi.com"

objCDO.Subject = subject
This sets the subject of the email to the variable that was defined above.

objCDO.Body = "Easy CGI ASP Test Form: " & vbcrlf & _
"------------------------" & vbcrlf & vbcrlf & _
"First name: " & request.form("fname") & vbcrlf & vbcrlf & _
"Last name: " & request.form("lname") & vbcrlf & vbcrlf & _
"Email: " & request.form("email") & vbcrlf & vbcrlf & _
"Comments: " & request.form("comments") & vbcrlf

Here we set the body of the email to be sent. It takes the information that was passed in from the form and puts some line breaks in between them.

On Error Resume Next
Tells the script to continue executing if an error occurs.

objCDO.Send
The most useful line, this actually sends the email.

The next lines display that the email was sent or an error if it occurred.

Else
This tells the code to output the form if the form was not submitted.

The rest of script is the HTML for displaying the form.

--Here is the full script, the filename should be: cdoemail.asp--

<%
'Easy CGI Test Form for sending email via CDONTS

'Change these values to test the form
'-------------------------------------
Dim toAddress, subject
toAddress = "support@easycgi.com"
subject = "Thanks for testing this great Easy CGI Form!"
'-------------------------------------

'Check to see if the form was submitted
if request.form("submit") <> "" Then

'if it was then send the email
Set objCDO = Server.CreateObject("CDONTS.NewMail")
objCDO.From = request.form("email") ' Specify sender's address
objCDO.To = toAddress
objCDO.Subject = subject
objCDO.Body = "Easy CGI Test Form for CDONTS: " & vbcrlf & _
"------------------------" & vbcrlf & vbcrlf & _
"First name: " & request.form("fname") & vbcrlf & vbcrlf & _
"Last name: " & request.form("lname") & vbcrlf & vbcrlf & _
"Email: " & request.form("email") & vbcrlf & vbcrlf & _
"Comments: " & request.form("comments") & vbcrlf

On Error Resume Next
objCDO.Send()

If Err <> 0 Then
Response.Write "Error encountered: " & Err.Description
Else
Response.Write "Thank you for submitting your information!"
End If
Set objCDO = Nothing
Else
'if the form was not submitted then output the form html
%>
<CENTER><B><FONT SIZE=+2>Easy CGI Test Form for sending email via CDONTS</FONT></U></B></CENTER></P>

<form action="cdoemail.asp" method="post">

<TABLE>
<TR>
<TH>First Name:</TH><TH><input type="text" name="fname" align ="right" size=40></TH>
</TR>
<br>
<TR>
<TH>Last Name:</TH><TH><input type="text" name="lname" align ="right" size=40></TH>
</TR>
<br>
<TR>
<TH>Email:</TH><TH><input type="text" name="email" align ="right" size=40></TH>
</TR>

<TR>
<TH>Comments:</TH><TH><TEXTAREA name="comments" rows="8" cols="30" align ="right"></TEXTAREA>
<TR>

</TABLE>
<input type="submit" value="Submit" name="submit">
</form>

<%
End If
%>

--End Script--

<< Back

 
 
 
Copyright © 2004 Creative Brain   || Home | Digital Photo Album | Online Store | Template Depot | Career | Help & Support | Contact Us