PHP Email Form with Captcha
In this tutorials, we will create a email form with captcha using php, mysql. This script contains 3 php files and 1 image
- Email Form (contact.php) - this files contains text fields like from, subject, message etc.
- captcha file code (captcha.php) - this file contains captcha code
- email script (thanks.php) - when email form will submitted, form will redirect on this page and this file will suggested either email will be send or not?
- captcha_image.jpg
contact.php
The mainly file contains following text field.
- Name
- Subject
- Message
- Captacha Image
- Captcha Field
- Submit Button
<h1>Contact Us </h1>
<form method="post" action="thanks.php">
<strong>
<table width="800" border="0">
<tr>
<td>Name:</td>
<td><input name="name" type="text" class="box" id="name"></td>
</tr>
<tr>
<td>Email</td>
<td><input name="from" type="text" class="box" id="from"></td>
</tr>
<tr>
<td>Subject</td>
<td><input name="subject" type="text" class="box" id="subject"></td>
</tr>
<tr>
<td>Message</td>
<td><textarea name="message" cols="40" rows="10" id="message"></textarea></td>
</tr>
<tr>
<td>Image Verification</td>
<td><img src="captcha.php" /> <br />
<input name="number" type="text" id=number>
<br />
</td>
</tr>
<tr align="center">
<td colspan="2"><input name="submit" type="submit" value="Send" /></td>
</tr>
</table>
</form>
captcha.php
in this file, we will write code about captcha
Source Code
<?php
session_start();
$RandomStr = md5(microtime());// md5 to generate the random string
$ResultStr = substr($RandomStr,0,5);//trim 5 digit
$NewImage =imagecreatefromjpeg("captcha_image.jpg");//image create by existing image and as back ground
$LineColor = imagecolorallocate($NewImage,233,239,239);//line color
$TextColor = imagecolorallocate($NewImage, 255, 255, 255);//text color-white
imageline($NewImage,1,1,40,40,$LineColor); //create line 1 on image
imageline($NewImage,1,100,60,0,$LineColor); //create line 2 on image
imagestring($NewImage, 5, 20, 10, $ResultStr, $TextColor); // Draw a random string horizontally
$_SESSION['key'] = $ResultStr;// carry the data through session
header("Content-type: image/jpeg");// out out the image
imagejpeg($NewImage);//Output image to browser
?>
thanks.php
session_start();
$RandomStr = md5(microtime());// md5 to generate the random string
$ResultStr = substr($RandomStr,0,5);//trim 5 digit
$NewImage =imagecreatefromjpeg("captcha_image.jpg");//image create by existing image and as back ground
$LineColor = imagecolorallocate($NewImage,233,239,239);//line color
$TextColor = imagecolorallocate($NewImage, 255, 255, 255);//text color-white
imageline($NewImage,1,1,40,40,$LineColor); //create line 1 on image
imageline($NewImage,1,100,60,0,$LineColor); //create line 2 on image
imagestring($NewImage, 5, 20, 10, $ResultStr, $TextColor); // Draw a random string horizontally
$_SESSION['key'] = $ResultStr;// carry the data through session
header("Content-type: image/jpeg");// out out the image
imagejpeg($NewImage);//Output image to browser
?>
thanks.php
This file contains two type code
- Captcha Code verification
- Email Script
<?php
session_start();
$name = $_POST["name"];
$from = $_POST["from"];
$subject = $_POST["subject"];
$message = $_POST["message"];
// remove the backslashes that normally appears when entering " or '
$name = stripslashes($name);
$from = stripslashes($from);
$message = stripslashes($message);
$subject = stripslashes($subject);
// check to see if verificaton code was correct
$key=substr($_SESSION['key'],0,5);
$number = $_POST['number'];
if($number!=$key){
echo $number."<br>";
echo $key;
echo 'Invalid Captcha Code! please <a href='contact.php'>go back</a>';
}
else{
// if verification code was correct send the message and show this page
mail("you@email.com", $subject, $message, "From: $from");
// delete the cookie so it cannot sent again by refreshing this page
echo "Email Sent Successfully";
}
?>
session_start();
$name = $_POST["name"];
$from = $_POST["from"];
$subject = $_POST["subject"];
$message = $_POST["message"];
// remove the backslashes that normally appears when entering " or '
$name = stripslashes($name);
$from = stripslashes($from);
$message = stripslashes($message);
$subject = stripslashes($subject);
// check to see if verificaton code was correct
$key=substr($_SESSION['key'],0,5);
$number = $_POST['number'];
if($number!=$key){
echo $number."<br>";
echo $key;
echo 'Invalid Captcha Code! please <a href='contact.php'>go back</a>';
}
else{
// if verification code was correct send the message and show this page
mail("you@email.com", $subject, $message, "From: $from");
// delete the cookie so it cannot sent again by refreshing this page
echo "Email Sent Successfully";
}
?>
in next we will learn about form validation and email validation.
