|
ASP Form to E-mail script
The most common use for e-mail within a
web site is to send the results obtained
from a form on the web site, the script
below builds on the previous
tutorial 'ASP Mail Script' which you
should read first.
In the previous tutorial you saw the basics
of sending an e-mail using AspEmail, sending
out the results of a form is very much the
same except that we use the contents of
the submitted form fields when creating
the mail.
For this example we'll use this simple
contact form :
The html code for which is as follows :
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
</head>
<body>
<form name="form1" method="post"
action="process.asp">
<table border="0" align="center"
cellpadding="2" cellspacing="0">
<tr>
<td>Contact Name</td>
<td width="20" align="center">:</td>
<td><input name="name"
type="text" id="name"></td>
</tr>
<tr>
<td>E-mail Address</td>
<td align="center">:</td>
<td><input name="email"
type="text" id="email"></td>
</tr>
<tr>
<td>Your Query</td>
<td align="center">:</td>
<td><input name="query"
type="text" id="query"></td>
</tr>
<tr>
<td> </td>
<td align="center"> </td>
<td align="center"><input
type="submit" name="Submit"
value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>
The form has three fields : name, email
and query which we'll use in creating our
e-mail.
The form when submitted is posted to another
script 'process.asp' and it's this script
that will read the form values and create
our e-mail, that script is as follows :
<%
' Retrieve values
from form fields and set as variables
formname = Request.Form("name")
formemail = Request.Form("email")
formquery = Request.Form("query")
' Create the AspEmail
message object
Set Mail = Server.CreateObject("Persits.MailSender")
' Set the from Name
and E-Mail address using values retrieved
from the form
Mail.From = formemail
Mail.FromName = formname
' Add the e-mail
recipient address - replace values within
the quotes with your own
Mail.AddAddress "you@yoursite.co.uk"
' Set the subject
for the e-mail
Mail.Subject = "Form submitted from
web site"
' Create a string
called bodytxt and build it line by line
using values from the form
Bodytxt = "Details of Form submission
:" & VbCrLf & VbCrLf
Bodytxt = Bodytxt & "Contact Name
: " & formname & VbCrLf
Bodytxt = Bodytxt & "E-Mail Address
: " & formemail & VbCrLf
Bodytxt = Bodytxt & "Query Entered
: " & formquery
' Set body text
for the e-mail to the Bodytxt string we
built
Mail.Body = Bodytxt
' The mail server
requires that we authenticate so supply
username and password
Mail.Username = "me@mysite.co.uk"
Mail.Password = "password"
' The e-mail is
now ready to go, we just need to specify
the server and send
Mail.Host = "smtp.dotnetted.co.uk"
Mail.Send
' Mail is sent -
tidy up and delete the AspEmail message
object
Set Mail = Nothing
%>
Explanation
The first three lines of the script use
the ASP instruction Request.Form to retrieve
the field values from the form as variables
to be used later in the script.
The next creates the AspEmail object is
as per the previous example.
The next two lines set the from Name and
E-mail address as those entered into the
form, this is useful as if you now reply
to the e-mail you receive from this script
you will be replying directly to your visitors
e-mail address as entered into the form.
The next two lines just set the e-mail
recipient address and E-Mail subject as
per the previous example.
The next section is usually the most confusing
if you've not done much work with ASP /
VBScript previously - what this section
does is first create a variable / string
called 'Bodytxt' and enter into it : "Details
of Form submission :".
The following lines ADD to this string
by using 'Bodytxt = Bodytxt &' the extra
text. The 'VbCrLf' is a carriage return
so we format the Bodytxt rather than having
one long string of text in our final e-mail.
The next line just sets the e-mail body
to our Bodytxt string and the remainder
of the script is as per our previous example.
Notes
This is a very basic script, for more information and
additional options available within AspEmail please
visit the authors web site : www.aspemail.com
|