iNET Interactive - Online Advertising Agency
          
   Home    Authors    About    Login    Contact Us
   Search:   
Advanced Search     
  Articles

  ASP (26)
  ASP.NET (19)
  C and C++ (4)
  CFML (2)
  CGI and Perl (16)
  Flash (2)
  Java (7)
  JavaScript (28)
  PHP (92)
  MySQL (13)
  MSSQL (3)
  HTML (34)
  SEO (9)
  Visual Basic (12)
  CSS (13)
  SSI (5)
  XML (12)
  C# (14)

  Developer News

May 15, 2008
Reader Question - Would you host your client's work on your website?
About
 
May 15, 2008
How to Create an Ajax Autocomplete Text Field: Part 6
WebReference.com
 
May 14, 2008
Poll: Are the browser safe colors still needed?
About
 
May 14, 2008
Google Doctype launched
About
 
May 14, 2008
Web Editor Reviews - 6 New Reviews
About
 
May 14, 2008
Build Beautiful Buttons in Photoshop, Part I
SitePoint
 
Courtesy of moreover.com
 
Want to receive new articles via e-mail? Click here!
/Home /ASP

Sending an email with data from an HTML Form (Regardless of the form’s amount of fields) 

  Views:    8585
  Votes:    9
by Romeo Marquez 11/27/03 Rating: 

Synopsis:

For the past 2 years i have been formally teaching ASP at my Alma Mater and since then i have been asked quite a lot of interesting questions regarding ASP and its application, I’ll discuss some of those topics here.
Pages: 
The Article

By using Request.Form and a For Each  together we can obtain the name of a field and

its correspondent value and then add it to a string.

This combination is actually great since it enables us to use the same piece of code regardless

of how many fields are in the form.

 

The following code will add to a string named message ALL the data captured by the form.

 

For Each x In Request.Form

message=message & x & ": " & Request.Form(x) & CHR(10)

Next

 

Request.Form contains an array with all the data posted regardless of the amount of fields in the form,

so we can trust this script to work whether we have a small contact form or a much larger form.

Later on the script, we can create a mail object to mail that string as part of the message of the body.

 

Now for the mailing part, we have several options.

Since I have clients with many different hosting services, you might need to work with whatever

mail object the hosting server provides; I’ll give an example using a couple of Mail objects widely

used on today’s market

 

            Set Mail = CreateObject("Persits.MailSender")

            Mail.Host = “mail.yourhost.com”

            Mail.From = request.form("Email")

            Mail.FromName = request.form("Name")

            Mail.AddAddress "romeo@jaguares.com"

            Mail.Subject = "Contact Form @ Jaguares.com"

            Mail.IsHTML = FALSE

            Mail.Body = message   ‘This is the variable with all the data collected form the form

            On Error Resume Next

            Mail.Send

            If Err <> 0 Then

                        response.write "<br><br>"&Err.Description

            End If

            set Mail = Nothing

            response.write “Finished!”

 

Here’s exactly the same functionality but with another mail object.

set Mail = Server.CreateObject("Bamboo.MAIL")

Mail.Server = “mail.yourhost.com”

Mail.Rcpt = "romeo@jaguares.com"

Mail.From = request.form("Email")

Mail.FromName = request.form("Name")

Mail.Subject = "contact from @ Jaguares.com"

Mail.Message = message ‘This is the variable with all the data collected form the form

On error resume next

Mail.Send

If err then response.Write err.Description

Set Mail = Nothing

            response.write “Finished!”

 

 

Remember that the On Error Resume Next prevents your script from abrupt termination in

case something was wrong with the Send method in the mail object.

 

Here’s the code to both files required in order to send an email trough an asp file

 

  1. Create contact.html with the following code

<html>

<head>

<title>Example 1</title>

</head>

<body>

<form method="post" action="SendMail.asp">

<b>Name:</b><br><input type="text" size="53" name="nombre">

<p><b>E-mail:</b><br><input type="text" size="28" name="Email">

<p><b>Phone:</b><br><input type="text" size="20" name="phone">

<p><b>Message:</b><br>

<textarea rows="6" cols="40" name="message"></textarea>

<p><input type="submit" value="Submit">

</form>

</body>

</html>

 

2. Create SendMail.asp with the following code:

 

<%

For Each x In Request.Form

message=message & x & ": " & Request.Form(x) & CHR(10)

Next

 

            Set Mail = CreateObject("Persits.MailSender")

            Mail.Host = “mail.yourhost.com”

            Mail.From = request.form("Email")

            Mail.FromName = request.form("Name")

            Mail.AddAddress "romeo@jaguares.com"

            Mail.Subject = "Contact Form @ Jaguares.com"

            Mail.IsHTML = FALSE

            Mail.Body = message   ‘ This is the variable with all the data collected form the form

            On Error Resume Next

            Mail.Send

            If Err <> 0 Then

                        response.write "<br><br>"&Err.Description

            End If

            set Mail = Nothing

            response.write “Finished!”

            %>

 

Regards, Romeo Marquez Guzman

Questions? Write me, romeo@gelattina.com

www.gelattina.com

Pages: 

Similar/related articles:


 
  Sponsors