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
For this example we'll use this simple
contact form (just an example, won't work!):
The html code for which is as follows :
<form name="form1" method="post"
<table border="0" align="center"
<td width="20" align="center">:</td>
<td align="center"> </td>
The form has three fields : name, email
and query which we'll use in creating our
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
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
' Set the subject
for the e-mail
Mail.Subject = "Form submitted from
' 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
Mail.Body = Bodytxt
' The mail server
requires that we authenticate so supply
username and password
Mail.Username = "firstname.lastname@example.org"
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 is sent -
tidy up and delete the AspEmail message
Set Mail = Nothing
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.
This is a very basic script, for more information and
additional options available within AspEmail please
visit the authors web site : www.htmlemail.com