By: D Evans

Make a generic email form processor

This code will process any form submitted and email the submitted form fields excluding the submit, the redirect, the hidden, and the required fields; to the designated recipient.

This can be used for multiple web sites or multiple forms. The redirect is used to send the form user back to their own web site. I placed it in an accessible folder to all users, and gave them a Url to link to it if they wished to use the form. The redirect then had to contain a to go back to their site. This makes it seamless.

For your own web site if you have multiple forms just refer the action to the place where you have the form processor. The url tag can then just have a page name you want them to go to after it is processed EX: index.html 

Or The url tag is optional. 

Create any html form.
Set the form action to go to your form processor file.

Make sure the form contains a required email field

Include 3 hidden fields:

recipient = who you want the email to go to
subject = the subject in the email you are sent
redirect = The page the form submitter is sent to after submission.

<form action=?email_form.cfm? method=?post?>
   <input type=
?hidden? name=?recipient? value=?>
   <input type= ?hidden? name=?subject? value= ?Add to email list?>
   <input type=
?hidden? name=?redirect? value=?>
   <input type=
?hidden? name=?email_required? value=?You must enter an email address to submit this form?>
   Name: <input type=?text? name=?yourname? value=??><br>
   Email Address: <input type=?text? name=?email? value=??><br>
   <input type=?submit? name=?submit? value=?Add me to your email list?>

3Of course this does need pretty formatting

Make the email_form processor file.

The if statement is used to exclude the hidden, required ,redirect and submit fields. To not submit any empty fields add or #fields# is "" to the if statement 

<cfmail to="#form.recipient#" from="" subject="#form.subject#" type=?html?>
#form.subject# <br><br>
<cfloop index="thefield" list="#form.fieldnames#">
    <cfset fields=
"#thefield# = #evaluate(thefield)#">
    <CFIF #fields# contains "submit" or #fields# contains "required"
            OR #FIELDS# contains "recipient" or #fields# contains "subject" 
            or #FIELDS# contains


<cflocation url="#form.redirect#" addtoken="No">

Email Results:

About This Tutorial
Author: D Evans
Skill Level: Intermediate 
Platforms Tested: CF5
Total Views: 93,102
Submission Date: December 13, 2002
Last Update Date: June 05, 2009
All Tutorials By This Autor: 4
Discuss This Tutorial

Website Designed and Developed by Pablo Varando.