Search Engine Optimization Services

Create a Mailform

Mail Form Example With PHP Function

Home » SEO Tutorials » Create a Mailform

Create Your Own Mailform In 3 Easy Steps!


You can use this code SEO-WATCH has developed to create your own contact mailform for your website. The mailform contains a confirmation page which will be displayed on the same page after the form is send.


You can view the demo SEO-WATCH has created by clicking on » Demo Contact Form.


1. Copy the code below and paste it into any HTML editor, start at line one there must be no space at top. Save the file as contact.php. Change $to = "youremail@xxx.com"; //your email address
and fill in your own email address.

Save it and upload the file to your web server where your index.php is located.

You can test it and receive the mail with data in your mailbox.
If you wish to have a CAPTCHA you can proceed with step 2.

<?php

/** File: contact.php
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* @link http://www.seo-watch.com
* @copyright 2010 Visual Orange Co., Ltd.
*
*/

session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Mail Form Free Contact Us Form</title>
<!-- A bit javaScript to check characters in comment box -->
<script type="text/javascript">
<!--
function textCounter(field, countfield, maxlimit) {
if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else
countfield.value = maxlimit - field.value.length;
}
//-->
</script>
<style type="text/css">
body {
font: normal 0.85em Verdana, Helvetica, sans-serif;
}
.error {
color: #FF0000;
}
form {
margin: 0px;
padding:0px;

}
table {
width: 600px;
white-space: nowrap;
}
input {
height: 19px;
border: 1px solid #333333;
}

</style>
</head>

 


<body>
<?php
$to = "youremail@xxx.com"; //your email address
if ( !isset( $_POST['submit'] ) )
{
showForm();
} else { // when the form is submitted we send the values
// in case you remove fields in the form you have to remove them here too

$gender = $_POST['gender'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$street = $_POST['street'];
$city = $_POST['city'];
$zip = $_POST['zip'];
$country = $_POST['country'];
$comment = $_POST['comment'];

$error = 0;
$errstr = array();
// all fields listed here are required to be filled in by user or selected
// You can remove those that are not required to be filled in
if(empty($_POST['gender'])) {
$error = 1;
$errstr[] = "Please select your title"; // knowing if its a man or woman has an advantage
}
if(empty($_POST['firstname'])) {
$error = 1;
$errstr[] = "Please enter your first name";
}

if(empty($_POST['lastname'])) {
$error = 1;
$errstr[] = "Please enter your last name";
}

if(!preg_match("/^(?:[\w\d]+\.?)+@(?:(?:[\w\d]\-?)+\.)+\w{2,4}$/", $_POST['email'])) {
$error = 1;
$errstr[] = "Please enter a valid e-mail address"; // check for valid email
}
if(empty($_POST['phone'])) {
$error = 1;
$errstr[] = "Please enter your phone number";
}
if(!ctype_digit($_POST['phone'])) {
$error = 1;
$errstr[] = "Numbers only at phone number. Dashes, spaces - or other signs are not allowed!"; // check if only digits are entered
}
if(empty($_POST['street'])) {
$error = 1;
$errstr[] = "Please enter the street and housenumber";
}
if(empty($_POST['city'])) {
$error = 1;
$errstr[] = "Please enter the city.";
}
if(empty($_POST['zip'])) {
$error = 1;
$errstr[] = "Please enter the zip code.";
}

if(empty($_POST['country'])) {
$error = 1;
$errstr[] = "Please select the country you live in.";
}
// put here the lines of validation for the captcha

// if you want to implement it


if ($error == 1) {
echo "<div class='error'>\n";
foreach($errstr as $err) {
echo "<li> " . $err . "</li>\n";
}
echo "</font>\n</div>\n<br />\n\n"; //we display all errors on top of the form

showForm(); //we found errors so we show form again

}
else { // no errors! We send the email to your mailbox

@mail($to, "Inquiry received at yourdomain " . $_POST['subject'],
"Inquiry form submitted,\nOn " . date("l dS \of F Y h:i:s A") . "\nTitle: " . $_POST['gender'] . "\nName: " . $_POST['firstname'] . " " . $_POST['lastname'] . "\nE-mail: " . $_POST['email'] . "\nPhone: " . $_POST['phone'] . "\nStreet and house number: " . $_POST['street'] . "\nCity: " . $_POST['city'] ."\nZip: " . $_POST['zip'] ."\nCountry: " . $_POST['country'] ."\n\nComment:\n" . stripslashes($_POST['comment']), "From: " . $_POST['email']);

// we display the confirmation message

echo "\n<br /><strong>Thank you ". $gender ."&nbsp;"; echo $_POST['lastname'].", </strong><br /><br />
We have received your request successfully. We will contact you a.s.a.p.<br />
<br />
<br />
With kindest regards,<br /><br />
put here your name or company name</strong><br /><br />";

}
}
//we create the form

function showForm()
{
$_POST['message'] = isset( $_POST['message'] ) ? htmlspecialchars( $_POST['message'] ) : '';
?>
Contact Form<br /><br />

More text here.<br />
<br /> All required fields are marked with an <span class="red">*</span><br /><br />
<br />
<form name="message" method="POST" id="message">
<table border="0" cellspacing="0" cellpadding="5" width="500">
<tr>
<td colspan="2" class="seoheader"><strong>Contact person</strong></td>
</tr>

<tr>
<td>Title: <span class="red">*</span></td>
<td><input name="gender" type="radio" value="Mr" <?php if ($_POST['gender'] == "Mr") echo "checked";?> /> Mr.
<input name="gender" type="radio" value="Mrs" <?php if ($_POST['gender'] == "Mrs") echo "checked"; ?> /> Mrs.</td>
</tr>
<tr>
<td>First name <span class="red">*</span></td>
<td><input name="firstname" type="text" maxlength="40" value="<?=$_POST['firstname']?>" /></td>
</tr>
<tr>
<td>Last name<span class="red">*</span></td>
<td><input name="lastname" type="text" id="lastname" maxlength="40" value="<?=$_POST['lastname']?>" /></td>
</tr>
<tr>
<td>E-mail <span class="red">*</span></td>
<td><input name="email" type="text" id="email" maxlength="80" value="<?=$_POST['email']?>" /></td>
</tr>
<tr>
<td>Phone <span class="red">*</span></td>
<td><input name="phone" type="text" id="phone" maxlength="13" value="<?=$_POST['phone']?>" /><br />
<br /></td>
</tr>
<tr>
<td>Street and house number <span class="red">*</span></td>
<td><input name="street" type="text" id="street" maxlength="50" value="<?=$_POST['street']?>" /></td>
</tr>
<tr>
<td>City <span class="red">*</span></td>
<td><input name="city" type="text" id="city" maxlength="50" value="<?=$_POST['city']?>" /><br />
<br /></td>
</tr>
<tr>
<td>Zip Code<span class="red">*</span></td><td><input name="zip" type="text" id="zipcode" maxlength="15" value="<?=$_POST['zip']?>" /><br />
</td>
</tr>
<tr>
<td class="line" colspan="2"><img src="../images/spacer.gif" alt="spacer" /></td>
</tr>
<tr>
<td>Country <span class="red">*</span></td>
<td><input name="country" type="text" id="country" maxlength="50" value="<?=$_POST['country']?>" /><br /> </td>
</tr>
<tr> <td>
Comment/question:</td>
<td>
<textarea name="comment" id="comment" cols="37" rows="5" onkeydown="textCounter(this.form.comment,this.form.remLen1,400);" onkeyup="textCounter(this.form.comment,this.form.remLen1,400);"><?=$_POST['comment']?>
</textarea>
<div align="right" style="margin-top:5px;">
<input readonly type="text" name="remLen1" size="3" maxlength="3" value="450" class="inputTextsmall" />
characters left &nbsp;</div>
<br /></td>
</tr>
<tr>
<td>PUT HERE YOUR CAPTCHA IMAGE </td>
<td><div style="margin-bottom:5px;">Enter security code:</div>
PUT HERE CAPTCHA TEXT FIELD </td>
</tr>
<tr>
<td colspan="2"><div style="text-align:center;"><input type="submit" name="submit" class="submit" value="Submit"></div>
<br /></td>
</tr>
</table>
</form>
<?php

}
?>


<div style="text-align:center;">Contact Form By <a href="http://www.seo-watch.com/>SEO Services</a></div>

</body>

</html>




2. Now go to » www.phpcaptcha.org/download/ and download the Securimage 2.0.1 (zip) [1.2 Mb], unzip it, and upload it to the same location where contact.php is located. It will look like this.













Now browse in the contact.php and search for this
// put here the lines of validation for the captcha
// if you want to implement it

(around line 133) select this and replace it with verification code in the box below.

if ( empty($_POST['code'] ) ) {
$errstr[] = "Please validate the image code";
} else {
include("securimage/securimage.php"); // check the name of the folder and path
$img = new Securimage();
$valid = $img->check($_POST['code']);
if (!$valid) { $errstr[] = "The security code you entered was incorrect";
}
}



3. Search for
<tr> <td>PUT HERE YOUR CAPTCHA IMAGE </td>
<td><div style="margin-bottom:5px;">Enter security code:</div> PUT HERE CAPTCHA TEXT FIELD </td>
</tr>

(around line 248) best is you select the complete row <tr> </tr> and replace it with code in the box below.

<tr>
<td><br />
<img id="siimage" align="left" style="padding-right: 5px; border: 0" src="securimage/securimage_show.php?sid={sid};" />
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="19" height="19" id="SecurImage_as3" align="middle">
<param name="allowScriptAccess" value="sameDomain" />
<param name="allowFullScreen" value="false" />
<param name="movie" value="securimage/securimage_play.swf?audio=securimage/securimage_play.php&bgColor1=#777&bgColor2=#fff&iconColor=#000&roundedCorner=5" />
<param name="quality" value="high" />
<param name="bgcolor" value="#ffffff" />
<embed src="securimage/securimage_play.swf?audio=securimage/securimage_play.php&bgColor1=#777&bgColor2=#fff&iconColor=#000&roundedCorner=5" quality="high" bgcolor="#ffffff" width="19" height="19" name="SecurImage_as3" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>
<!-- pass a session id to the query string of the script to prevent ie caching -->
<br />
<a tabindex="-1" style="border-style: none" href="#" title="Refresh Image" onClick="document.getElementById('siimage').src = 'securimage/securimage_show.php?sid=' + Math.random(); return false"><img src="securimage/images/refresh.gif" alt="Reload Image" border="0" onClick="this.blur()" align="bottom" /></a>
<br /></td>
<td><br />
Security Code:
<br /> <input type="text" name="code" /> </td>
</tr>


Save the file, upload it. Now you are ready to test your form online.

If you any questions? Feel free to » Contact Us.

What Does SEO Cost?

SEO Pricing
Best Price Quality Relationship

Fixed Monthly Rates

Cover Your Internet Marketing Needs!

2005 - 2014 Copyright © SEO-WATCH All Rights Reserved. Web Design:: SEO-WATCH Co., Ltd. Thailand   SEO For Yahoo   SEO For Google