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>
<title>form to email script</title>
<form method="POST" action="form_ac.asp" name="form1">
<table width="75%" border="0" cellspacing="0" cellpadding="0">
<td><font face="Verdana, Arial, Helvetica, sans-serif" size="2">name:</font></td>
<input type="text" name="name">
<td><font face="Verdana, Arial, Helvetica, sans-serif" size="2">email:</font></td>
<input type="text" name="email">
<td><font face="Verdana, Arial, Helvetica, sans-serif" size="2">message:</font></td>
<textarea name="message" cols="40" rows="5"></textarea>
<input type="submit" name="Submit" value="Submit">
<input type="reset" name="Reset" value="Reset">
Call this page form_ac.asp (the ac is short for acquire as it will be receiving values)
<% @ Language="VBscript" %>
<% Option Explicit %>
'receive the values sent from the form and assign them to variables...
'declare the variables that will receive the values
'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
'create the mail object and send the details
NewMailObj.From = "firstname.lastname@example.org"
NewMailObj.To = "email@example.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
'Close the email object and free up resources
Set NewMailObj = nothing
<font face="Verdana, Arial, Helvetica, sans-serif" size="2">The email was sent.</font>
You may be interested in reading our tutorial 'Insertform content into a database'
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More Web Development Articles
More By Developer Shed