Simple PHP email form with basic spam protection CAPTCHA

There are two pieces of code – the one which will display your email form, and a block of code to add into your “mailer” page – a page dedicated to sending the messages and displaying confirmations and errors.

Feel free to edit or tweak the code, download the images to your own server and use however you wish.

The form

<!-- email box ~ -->
<div style="width:400px;text-align:right;padding:0px 0px 0px 0px; margin:0px 0px 0px 0px;">
<form action="send.php" method="post" style="padding:0px 0px 0px 0px; margin:0px 0px 0px 0px;">
<p style="padding:0px 0px 0px 0px; margin:0px 0px 0px 0px;">
Name: <input type="text" name="email_name" size=20><br>
Email: <input type="text" name="email_email" size=20>
<?php
$one = 1;
$two = 0;
$three = 0;
$four = 0;
while ($one % 2) $one = rand (10000,99999);
while (!($two % 2)) $two = rand (10000,99999);
while (!($three % 2)) $three = rand (10000,99999);
while (!($four % 2)) $four = rand (10000,99999);
?>
<br>Are you a human or internet virus?
<br>Choose the odd one out:
</p>
<div style="padding:0px 0px 0px 0px; margin:0px 0px 0px 0px;">
<input type="radio" name="email_valid" value="<?php echo $four ?>" Checked=1> <img src="https://pushka.com/p/i/sys/lion.png" alt="This is a public domain image">
<input type="radio" name="email_valid" value="<?php echo $two ?>"> <img src="https://pushka.com/p/i/sys/elephant.png" alt="This is a public domain image">
<input type="radio" name="email_valid" value="<?php echo $one ?>"> <img src="https://pushka.com/p/i/sys/lifter.png" alt="This is a public domain image">
<input type="radio" name="email_valid" value="<?php echo $three ?>"> <img src="https://pushka.com/p/i/sys/penguin.png" alt="This is a public domain image">
</div>
<p style="width:200; text-align:left;padding-bottom:0px; margin:0px 0px 0px 0px;padding-top:0px; padding-left:150px;">Message:</p>
<textarea rows="7" cols="30" name="email_msg" style="padding:0px 0px 0px 0px; margin:0px 0px 0px 0px;">
</textarea><br>
<input type="submit" value="Send">
</form>
</div>

The sender and confirmation message generator (should be in the main text region of your regular website theme)

<div title="main"><h1>Send</h1>
<div style="width:400px;"><br /><br />
<?php
$name = $_POST["email_name"];
$email = $_POST["email_email"];
$valid = $_POST["email_valid"];
$message = $_POST["email_msg"];
$error = 0;
if (strlen($email) == 0) {
echo "<img src='https://pushka.com/p/i/sys/cross.png'> Please enter an email<br>";
$error =1;}
elseif (strpos($email,"@") === false | strlen($email) < 6){
echo "<img src='https://pushka.com/p/i/sys/cross.png'> Please enter a valid email in the form X@X.XX<br>";
$error=1;}
if (strlen($message)==0){
echo "<img src='https://pushka.com/p/i/sys/cross.png'> Please enter a message<br>";
$error=1;}
if ($valid%2){
echo "<img src='https://pushka.com/p/i/sys/cross.png'> There was an error with the image validation, please select the odd one out.<br>";
$error=1;}
if (strlen($name) == 0) $name = $email;
$message = "This message was sent from pushka.com/p - from " . $name . " \n ---------------------------- \n \n " . $message . " \n \n ---------------------------- \n \n \n";
if ($error==0){
echo "<img src='https://pushka.com/p/i/sys/tick.png'> Email Sent<br><br><FORM><INPUT TYPE='button' VALUE='Back' onClick='history.go(-1);return true;'> </FORM> <br>";
$to = 'you@mail.com';
$subject = 'Message from pushka.com/p';
$headers = 'From: ' . $email . " \r\n" .
'Reply-To: ' . $email . " \r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
else{
echo "<br><FORM><INPUT TYPE='button' VALUE='Back' onClick='history.go(-1);return true;'> </FORM> <br>";
}
?>
<br><br>
</div>
</div>

 Here is an example

The emails are sent to the Cosmic Owl, who may or may not ever respond to you

[widgets_on_pages id=”PHPemailForm”]

You can have both blocks of code on the same page if you have action=””