DotNetted - Windows hosting specialists
 
 
   
General Support
Domain Names
Default Documents
SSL Shared Cert
ASP 500 error message
Pre-installed Files
Publishing Your Site
Uploading with FTP
FrontPage/Visual Studio
Using DreamWeaver
E-Mail
Greylisting (important)
Setup pop3 and smtp
Using webmail
E-mail administration
Configure spam filters
Databases
Access Database
SQL Server 2005
SQL Server 2000
Scripting
ASP Mail Script
ASP Form To E-Mail
ASP Send Secure E-Mail
Other ASP Components
Session State for .NET
Send mail with .NET 1.1
Send mail with .NET 2+
Upload file using .NET
Contact tech support

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 :

Contact Name :
E-mail Address :
Your Query :
   

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>&nbsp;</td>
<td align="center">&nbsp;</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 the additional options available within AspEmail please visit the authors web site : www.aspemail.com

reliability and security
separate application for each site
unique 'sandboxed' user per site
windows 2008 / IIS 7 web hosting
windows 2003 / IIS 6 web hosting
virus / spam protection
all incoming e-mail virus checked
fully configurable spam filtering
scripting support
full support - asp.net version 1.1
full support - asp.net version 2.0
full support - asp.net version 3.0
full support - asp.net version 3.5
full support - asp.net version 4.0
full support - asp 'classic' 3.0
quality asp components installed
complete e-mail service
pop3 and smtp e-mail services
industry leading webmail interface
web based administration interface
development software
compatible with dreamweaver
compatible with visual studio .net
compatible with MS frontpage
database support
MS access databases included
sql server 2000 database £6 mth
sql server 2005 database £6 mth
statistics reporting
comprehensive web based stats
pre-defined or custom stats reports
optional SSL support
shared SSL access just £1 / month
your own SSL cert just £5 / month
 
Copyright © DotNetted - All Rights Reserved Prices are subject to VAT @ 17.5%