|
Forms - Sending Results via EMail by Charles Carroll
Sometimes it makes sense to just email all the form results
to someone. But the "mailto:" method used by many Web developers is not the best
way as it has the following serious limitations:
- forms sent this way are difficult to read by a human recipient
because of the encoded way it is sent
- Not all browsers support mailto (Netscape does, several IE
versions before 4.0 do not, many browsers do not)
- The user may have a faulty email configuration that keeps the
browser from sending the mail
Server side mail has none of these limitations. Here is a
form that will be sent to you provided you change the variables at the top of the code.
This example uses ASPmail, which is available for a modest price at: http://www.serverobjects.com
Here is a sample form that will be sent via email:
filename=/learn/test/formToBeMailed.asp
<%
' change these to reflect where form should go
fromName="Whoever"
fromAddress="somestudent@activeserverpages.com"
toName="Charles M. Carroll"
toAddress="cc@thebestweb.com"
subject="form send mail tutorial"
relay="mail.innerhost.com"
%>
<html><head>
<title>FormToBeMailed.asp</title>
</head><body bgcolor="#FFFFFF">
<form action="FormToBeMailedrespond.asp" method="GET">
Fill Out This Form For Us:<p>
First Name -> <input NAME="NameFirst" size="20"><br>
Last Name -> <input NAME="NameLast" size="20"><br>
Country -> <input NAME="Country" value="USA" size="20"><br>
State -> <input NAME="State" MaxLength="2" size="2"><br>
email copy to -> <input NAME="emailcopy" MaxLength="40" size="40"><br>
<input type="submit"><input type="reset">
<% ' do not touch these lines. Needed to send mail! %>
<input type="hidden" name="mail-from" value="<%=fromName%>">
<input type="hidden" name="mail-fromAddress" value="<%=fromAddress%>">
<input type="hidden" name="mail-to" value="<%=ToName%>">
<input type="hidden" name="mail-toaddress" value="<%=toaddress%>">
<input type="hidden" name="mail-subject" value="<%=subject%>">
<input type="hidden" name="mail-relay" value="<%=relay%>">
</form>
</body></html>
Here is the generic form responder that can
be used with this or any form:
filename=/learn/test/formToBeMailedrespond.asp
<html><head>
<title>serverobjectsmailrespond.asp</title>
</head><body bgcolor="#FFFFFF">
<%
' ASPMail(tm) from http://www.serverobjects.com
' is not part of ASP per se,
' but a excellent third party component
my_from=request("mail-fromName")
my_fromAddress=request("mail-fromaddress")
my_to=request("mail-toName")
my_toAddress=request("mail-toaddress")
my_subject=request("mail-subject")
my_relay=request("mail-relay")
Set Mailer = Server.CreateObject("SMTPsvg.Mailer")
Mailer.RemoteHost = my_relay
Mailer.FromName = my_from
Mailer.FromAddress = my_fromAddress
Mailer.AddRecipient my_to, my_toaddress
Mailer.Subject = my_subject
for each whatever in request.querystring
If instr(whatever,"mail-")=0 then
Mailer.BodyText = whatever & "=" & vbcrlf
Mailer.BodyText = request.querystring(whatever) & vbcrlf & vbcrlf
end if
next
for each whatever in request.form
If instr(whatever,"mail-")=0 then
Mailer.BodyText = whatever & "=" & vbcrlf
Mailer.BodyText = request.form(whatever) & vbcrlf & vbcrlf
end if
next
my_emailcopy=request("emailcopy")
If my_emailcopy="" then
else
Mailer.AddRecipient "form filler",my_emailcopy
end if
If Mailer.SendMail then
Msg = "mail sent sucessfully!"
Else
Msg = "mail was not sent sucessfully<br>"
msg = msg & mailer.response & "<br>"
End If
response.write Msg
%>
</body></html>
|