Nik's Technology Blog

Travels through programming, networks, and computers

Generic web form emailer using FOR EACH and CDONTs

The ASP code below will loop through all the requested form fields submitted to it and send a CDO email from your server to your email account with the values of all those fields.

PROCESS PAGE

<%

Dim cdoMail, Recipient, Subject, Redirect, Sender, item, MailText

Recipient = "my_account@nobody.com"

Subject = "Enquiry from website"

Redirect = "index.html"

Sender = "server@nobody.com"

  MailText = "Email received from my website"

for each item in request.form

  MailText = MailText & item & ": " & request.form(item) & vbcrlf

next

Set cdoMail = Server.CreateObject("CDONTS.NewMail")

  cdoMail.From = Sender

  cdoMail.To = Recipient

  cdoMail.Subject = Subject

  cdoMail.Body = MailText

  cdoMail.Send

Set cdoMail = Nothing

Response.Redirect Redirect

%>

For more information about the FOR EACH vbscript statement please see my previous post below.

Simple ASP Form Collection with FOR EACH

The code below shows how to grab form submitted data from a page by looping through all submitted form items using the FOR EACH vbscript code.

Note: The submit button does NOT have a NAME attribute, this is so it doesn't appear within the fields on the process page.

The code can be altered to loop through query string data aswell. Simply replace "for each item in request.form" with "for each item in request.querystring".

FORM PAGE

<html><head><title>Lazy Man's Form Collection using FOR EACH in ASP</title></head><body>

<form action="form_collection.asp" method="post"> <input type="text" name="Name">

<input type="text" name="Address">

<input type="text" name="Country">

<input type="text" name="Age">

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

</form>

</body></html>

PROCESS PAGE

<html><head><title>Form Collection</title></head><body>

<%

for each item in request.form

response.write "<b>" & item & ":</b> " & request.form(item) & "<br>"

next

%>

</body></html>