Form to Mail Script Examples

Get the script

Example One

Here is the content of one of my forms where I want the visitor to fill in details applying for an award. I have placed all of the field names in bold to make it easier for you to spot them.

<form action="email.php" method="post">
<input type="hidden" name="url" value="award/thanks.htm" />
<input type="hidden" name="subject" value="Web Site Construction Award" />
<table border="0" cellpadding="5" cellspacing="0" class="centre"><tr>
<td class="right">Your Name</td><td colspan="2">
<input type="text" name="name" value="" size="25" maxlength="40" /></td></tr>
<tr><td class="right">Your Email</td><td colspan="2">
<input type="text" name="replyemail" value="" size="25" maxlength="40" /></td></tr>
<tr><td class="right">The Website you are Submitting</td>
<td colspan="2"> <input type="text" name="web" value="" size="35" maxlength="60" /></td></tr>
<tr><td colspan="3" class="centre"><br />
<input type="hidden" name="owner" value="no" />
<input type="checkbox" name="owner" value="yes" /> I confirm that I am the owner of the web site.<br />
<input type="hidden" name="age" value="no" />
<input type="checkbox" name="age" value="yes" /> I confirm that I am not under 16 years old.<br /></td></tr>
<tr><td colspan="3" class="centre"><b>Description of your site</b><br />
<span class="small">(that we can use if you win an award)</span>
<br /> <textarea rows="5" cols="60"></textarea><br /><br />&nbsp;<br />
<textarea rows="10" cols="60">Anything else you wish to tell us about your site.</textarea></td></tr>
<tr><td colspan="2">&nbsp;</td><td>
<input type="button" value="Submit" />
<input type="reset" name="reset" value="Clear" /></td></tr>
</table></form>

The first two fields on this form are hidden meaning that the subject of the form and the address of the page to display after the form is emailed cannot be changed by the person filling out the form. The person has three input fields, two check boxes and two text fields that they can fill in and a submit button to select once they are done. In this instance the button calls a javascript validation routine to validate the data entered before passing it to the email script.

Let's say I enter Stephen in the name field, myemail@myhost.com in the email field, http://mysite.myhost.com in the web field, I check the age box but don't check the owner box, I enter Info about the site. in the description field, and leave the message field alone. The email would be sent to the default destination with a subject of "Web Site Construction Award". The content of the email would read as follows:

The following information was entered:
 
web : http://mysite.myhost.com
owner : no
age : yes
description : Info about the site.
 
Anything else you wish to tell us about your site.
regards,
Stephen
 
myemail@myhost.com (210.27.150.6)

This gives me all of the essential information that was entered into the form.

If this level of processing is all that you require then you are welcome to use the free version of my form to mail script.

Example Two

Let's suppose that we need to get the above email formatted somewhat better to give a neater email that we could forward to someone else. With the more advanced version of my script we can do this. Let's begin by adding three extra hidden fields at the top of the above form. We'll also change the value associated with the owner checkbox to make it fit with our new wording.

<input type="hidden" name="header" value=" " />
<input type="hidden" name="footer" value=" " />
<input type="hidden" name="template" value="Hi,\nMy name is ~name~ and I would like to submit the web site ~web~ for consideration for your web site award.\n\nI ~owner~ the owner of the site.\nI am not under 16 years old.\n\nPlease use the following description of the site if it wins:\n~description~\n\nThe following information is also relevant:" />
<input type="hidden" name="owner" value="an not" />
<input type="checkbox" name="owner" value="am" /> I confirm that I am the owner of the web site.<br />
 

The first two of these remove the default header and footer while the third provides the text into which the fields entered are to be substituted. We use \n to indicate that a plain text email should start a new line. With entering the same values as in example one our email would now read as follows:

Hi,
My name is Stephen and I would like to submit the web site http://mysite.myhost.com for consideration for your web site award.
 
I am not the owner of the site.
I am not under 16 years old.
 
Please use the following description of the site if it wins:
Info about the site.
 
The following information is also relevant:
Anything else you wish to tell us about your site.
regards,
Stephen
>

As you can see this email is a lot easier to read than the first.

Example Three

Let's suppose that we want to format the email using html instead of having plain text. To do this we add one extra hidden field and alter the hidden fields at the top of the form that we added in example two.

<input type="hidden" name="fmt" value="html" />
<input type="hidden" name="header" value="<html><head><title>Award Submission</title></head><body bgcolor="#ffffff">" />
<input type="hidden" name="footer" value="</p></body></html> " />
<input type="hidden" name="template" value="<p>Hi,<br />My name is ~name~ and I would like to submit the web site <a href='~web~'>~web~</a> for consideration for your web site award.</p><p>I ~owner~ the owner of the site.<br />I am not under 16 years old.</p><p>Please use the following description of the site if it wins:<br />~description~</p><p>The following information is also relevant:<br />" />

All we have done here is to recode the header, footer, and template using html and added a fmt hidden field to set the email format to html. The same output as example two will be produced but this time the email is html encoded instead of plain text and it has a light blue background.

Example Four

We're getting quite a few hidden fields by this point so let's move them into a separate file. We replace all of the hidden fields with just the one as follows:

<input type="hidden" name="include" value="myform.php" />

and we create a file called myform.php to set the other fields for us as follows:

<? $fmt ="html";
$header="<html><head><title>Award Submission</title></head><body bgcolor=\"#ffffff\">";
$footer="</p></body></html> ";
$template="<p>Hi,<br />My name is ~name~ and I would like to submit the web site <a href='~web~'>~web~</a> for consideration for your web site award.</p><p>I ~owner~ the owner of the site.<br />I am not under 16 years old.</p><p>Please use the following description of the site if it wins:<br />~description~</p><p>The following information is also relevant:<br />";
?>
and so on

You can of course further modify the html code to produce even more sophisticated emails derived from your visitor's form inputs including embedding images into the html using a comma separated list in the embed field and referencing them using ~embed0~ etc. in the html. You can also attach files to your email by specifying a comma separated list in the attach field. If your html is so sophisticated that you prefer to specify the template value in its own file and you don't need to append a separate message afterwards (you can still use ~field~ substitutions and call your free format text field something else) then you can pass that filename in the message field and specify the following code in your include file to process it:

<?  $fmt ="html";
$header="<html>";
$footer="</html> ";
$template = fread(fopen($message),filesize($message));
$message = "";
?>

The advanced version of the script also has the ability to pass the form fields on to the thankyou page so that you can include them in the display of that page.

Get the script

 

This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow
Donate