Tutorials

Email form contents

There are just a few simple steps to follow in order to do this.

You need to grab the form values and stuff them into variables, use those variables to construct your email body and finally, send the email.

This example will use CDONTS (Commonly known as CDO Mail) as the mailer but the same steps can be applied to whichever email component you have access to.

It is coded in ASP/VBScript.

Lets say we have four form elements to capture Name, Email address, Company and Enquiry details from the user - we will grab those values and stuff them into variables. (You don't need to do this, I just like to for ease of reading and updating my code).

My form elements are called NameEB, EmailEB, CompanyEB and EnquiryEB respectively.

I use EB on the end of the name to signify 'Edit Box'.
If it was a Radio Button form element, I would use RB - this is just my preference, not a rule!

1. Grab the form values

<%
Dim varName, varEmail, varCompany, varEnquiry
varName = Request("NameEB")
varEmail = Request("EmailEB")
varCompany = Request("CompanyEB")
varEnquiry = Request("EnquiryEB")
%>

Next we need to construct the email body using the variables we have just passed the form values into.

2. Construct the email body

What were going to do here is use a variable, strMessage, to build up the email body.

<%
Dim strMessage
strMessage = "This email contains the form contents that were submitted" & vbCRLF & vbCRLF
strMessage = strMessage & "Name: " & varName & vbCRLF
strMessage = strMessage & "Email: " & varEmail & vbCRLF & vbCRLF
strMessage = strMessage & "Company: " & varCompany & vbCRLF
strMessage = strMessage & "Enquiry: " & varEnquiry & vbCRLF

To explain...

vbCRLF is the Visual Basic constant for a Carriage Return and Line Feed.

We don't close off the ASP tags <% %> here because this section and the next section are really one piece of code.

All thats left for us to do is to send the email using our email component - in this case, CDONTS.

3. Send the email

First, we tell the server to create the mail object, then we tell it where it's going and what to send, then we destroy the mail object to clean up.

Set objMail = Server.CreateObject("CDONTS.NewMail")
objMail.To = "sales@mysite.co.uk"
objMail.bcc = "marketing@mysite.co.uk"
objMail.From = "mywebsite@mysite.co.uk"
objMail.Subject = "Email form contents"
objMail.Body = strMessage
objMail.Send
Set objMail = Nothing
%>

As you can see, we create the email object, tell it who it's going to, where it's from, give it a subject line and pass it the body we built up using the strMessage variable above.

We then send the email and clean up.

Notes

In this case I specified an email address as the 'from' address but you could just as easily have used the varEmail variable in there to make it look like the email came from the person that submitted the form.

For example:
You would replace this line objMail.From = "mywebsite@mysite.co.uk"
With this line objMail.From = varEmail

Be aware that doing this might cause the email to not be sent if the user supplies an incorrect email address.