Create a Feedback Form - Step III
In this part of the tutorial I will show you how to use CDONTS to email the form results to someone.
36. Open the feedback.asp page.
37. Go into Code View by clicking View > Code.
38. Look for the following code:
If (MM_editRedirectUrl <> "") Then
Response.Redirect(MM_editRedirectUrl)
End If
39. Insert a row before the Response.Redirect(MM_editRedirectURL), like the following.
If (MM_editRedirectUrl <> "") Then
Response.Redirect(MM_editRedirectUrl)
End If
40. Paste the following code in the row that you just inserted.
Dim strFrom, strTo, strCC, strBCC, strBody, strSubject, objMail
'Who is it from
strFrom = Request.Form("FirstName")
'Who is it to
strTo = "someone@yoursite.com"
'Who gets a carbon copy
strCC = ""
'Who gets a blind carbon copy
strBCC = ""
'What is the Subject Line
StrSubject = "Online Form Submission"
'What is in the body of the email
strBody = "The following info has been submitted" & vbCrLf & vbCrLf &_
"First Name: " & Request.Form("FirstName") & vbCrLf &_
"Last Name: " & Request.Form("LastName") & vbCrLf &_
"Company: " & Request.Form("Company") & vbCrLf &_
"Address: " & Request.Form("Address") & vbCrLf &_
"Address cont'd: " & Request.Form("Address1") & vbCrLf &_
"City: " & Request.Form("City") & vbCrLf &_
"State: " & Request.Form("State") & vbCrLf &_
"Zipcode: " & Request.Form("Zipcode") & vbCrLf &_
"Email: " & Request.Form("Email") & vbCrLf &_
"Telephone: " & Request.Form("Telephone") & vbCrLf &_
"Fax: " & Request.Form("Fax") & vbCrLf & vbCrLf &_
"Be sure to check the online backside"
'Create the mail object
Set objMail = CreateObject("CDONTS.NewMail")
'String for who is it from
objMail.From = strFrom
'String for who is it to
objMail.To = strTo
'String for who gets a carbon copy
objMail.CC = strCC
'String for who gets a blind carbon copy
objMail.BCC = strBCC
'String for the subject
objMail.Subject = strSubject
'String for the body
objMail.Body = strBody
'Send the mail
objMail.Send()
'Delete the mail object
Set objMail = Nothing
I guess that I could just leave it at that, but I'll try to explain the code line-by-line. First let me tell you what a few things mean.
A single quotation ' is a comment for ASP.
A & sign means just that, and.
A underscore _ means that the code is continued on the next line
vbCrLf is a carriage return line feed.
Dim strFrom, strTo, strCC, strBCC, strBody, strSubject, objMail
This declares the variables used in the code and reserves a space in memory for them.
'Who is it from
strFrom = Request.Form("FirstName")
This takes the FirstName input value and converts it to a string to be used later.
'Who is it to
strTo = "someone@yoursite.com"
This creates a string for the To address that will be used later.
'Who gets a carbon copy
strCC = ""
'Who gets a blind carbon copy
strBCC = ""
This creates an empty string for the CC and BCC that will be used later. You can add additional addressees here if needed.
strBody = "The following info has been submitted" & vbCrLf & vbCrLf &_
"First Name: " & Request.Form("FirstName") & vbCrLf &_
"Last Name: " & Request.Form("LastName") & vbCrLf &_
"Company: " & Request.Form("Company") & vbCrLf &_
"Address: " & Request.Form("Address") & vbCrLf &_
"Address cont'd: " & Request.Form("Address1") & vbCrLf &_
"City: " & Request.Form("City") & vbCrLf &_
"State: " & Request.Form("State") & vbCrLf &_
"Zipcode: " & Request.Form("Zipcode") & vbCrLf &_
"Email: " & Request.Form("Email") & vbCrLf &_
"Telephone: " & Request.Form("Telephone") & vbCrLf &_
"Fax: " & Request.Form("Fax") & vbCrLf & vbCrLf &_
"Be sure to check the online backside"
This creates a string for the body of the message to be used later. It takes the form data from the submitted form. Remember the &, vbCrLf, and the _ explanation earlier.
'Create the mail object
Set objMail = CreateObject("CDONTS.NewMail")
This creates the CDONTS NewMail object.
'String for who is it from
objMail.From = strFrom
This says who the email is from.
'String for who is it to
objMail.To = strTo
This says who the email is addressed to.
'String for who gets a carbon copy
objMail.CC = strCC
This says who gets a CC of the email.
'String for who gets a blind carbon copy
objMail.BCC = strBCC
This says who gets a BCC of the email.
'String for the subject
objMail.Subject = strSubject
This says what the subject of the email will contain.
'String for the body
objMail.Body = strBody
This says what the body of the message will contain.
'Send the mail
objMail.Send()
This sends the email.
'Delete the mail object
Set objMail = Nothing
And this deletes the CDONTS NewMail object.
So your final code should look like this:
If (MM_editRedirectUrl <> "") Then
Dim strFrom, strTo, strCC, strBCC, strBody, strSubject, objMail
'Who is it from
strFrom = Request.Form("FirstName")
'Who is it to
strTo = "someone@yoursite.com"
'Who gets a carbon copy
strCC = ""
'Who gets a blind carbon copy
strBCC = ""
'What is the Subject Line
StrSubject = "Online Form Submission"
'What is in the body of the email
strBody = "The following info has been submitted" & vbCrLf & vbCrLf &_
"First Name: " & Request.Form("FirstName") & vbCrLf &_
"Last Name: " & Request.Form("LastName") & vbCrLf &_
"Company: " & Request.Form("Company") & vbCrLf &_
"Address: " & Request.Form("Address") & vbCrLf &_
"Address cont'd: " & Request.Form("Address1") & vbCrLf &_
"City: " & Request.Form("City") & vbCrLf &_
"State: " & Request.Form("State") & vbCrLf &_
"Zipcode: " & Request.Form("Zipcode") & vbCrLf &_
"Email: " & Request.Form("Email") & vbCrLf &_
"Telephone: " & Request.Form("Telephone") & vbCrLf &_
"Fax: " & Request.Form("Fax") & vbCrLf & vbCrLf &_
"Be sure to check the online backside"
'Create the mail object
Set objMail = CreateObject("CDONTS.NewMail")
'String for who is it from
objMail.From = strFrom
'String for who is it to
objMail.To = strTo
'String for who gets a carbon copy
objMail.CC = strCC
'String for who gets a blind carbon copy
objMail.BCC = strBCC
'String for the subject
objMail.Subject = strSubject
'String for the body
objMail.Body = strBody
'Send the mail
objMail.Send()
'Delete the mail object
Set objMail = Nothing
Response.Redirect(MM_editRedirectUrl)
End If
41. Save the feedback.asp page.
You can download a copy of it here.
That is all there is to it.
Be aware that if you try to use the CDONTS on your local computer to send an email you are more than likely going to get an error message, unless you have it set up correctly, and that is outside the scope of any of these tutorials.