Sending Form Values to an email

Copy the 2 pages below and paste them into the same folder and then run form.html on your webserver (this won't work on your personal webserver, must be an internet server and support the CDONTS mail object).

If you fill in the text boxes on the form.html page and click submit then the values entered will be sent to form_ac.asp. Form_ac.asp will be the page that will receive and process the values entered into the text boxes.

In our example once the variables have been received by form_ac.asp an email will then be sent which holds the variables. The comments in the pages should explain the code.

Call this page form.html

<html>
<head>
<title>form to email script</title>
</head>
<body>

<div align="center">
<form method="POST" action="form_ac.asp" name="form1">
<table width="75%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>name:</td>
<td colspan="2">
<input type="text" name="name">
</td>
</tr>
<tr>
<td>email:</td>
<td colspan="2">
<input type="text" name="email">
</td>
</tr>
<tr>
<td>message:</td>
<td colspan="2">
<textarea name="message" cols="40" rows="5"></textarea>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td colspan="2">
<input type="submit" name="Submit" value="Submit">
<input type="reset" name="Reset" value="Reset">
</td>
</tr>
</table>
</form>
</div>
</body>
</html>

Call this page form_ac.asp (the ac is short for acquire as it will be receiving values)

<%@ Language="VBscript" %>
<% Option Explicit %>

<html>
<head>
<title>Message Sent</title>
</head>

<body>
<%
'declare the variables that will receive the values
'receive the values sent from the form and assign them to variables
'note that request.form("name") will receive the value entered into the textfield
'called name, and so with email and message
Dim name, email, message, NewMailObj
name=request.form("name")
email=request.form("email")
message=request.form("message")

'create the mail object and send the details
Set NewMailObj=Server.CreateObject("CDONTS.NewMail")
NewMailObj.From = "michael@codefixer.com"
NewMailObj.To = "whoever_you_want_to_send_it_to@hotmail.com"
NewMailObj.Subject = "New message sent.."
NewMailObj.Body = "the name you entered was " & name & _
"<br>the email was " & email & _
"<br>the message was " & message

'you need to add the following lines FOR the mail to be sent in HTML format
NewMailObj.BodyFormat = 0
NewMailObj.MailFormat = 0
NewMailObj.Send
'Close the email object and free up resources
Set NewMailObj = nothing
Response.write "The email was sent."
%>

</body>
</html>

You may be interested in reading our tutorial 'Insert form content into a database'
Learn how to text format an email

Get the best ASP hosting with DiscountASP.NET - great value, money back guarantee.

Advertisements



MembersPro

MembersPro PayPal - ASP Membership software

Plug and play ASP membership script that integrates with PayPal to let you charge recurring membership fees.

Global ASP.NET Hosting Leader - Click Here