# FREE Email Form - PHP

**URL:** <https://forum.kirupa.com/t/free-email-form-php/320691>\
**Category:** programming\
**Created:** [March 30, 2011, 2:25pm UTC](https://forum.kirupa.com/t/free-email-form-php/320691 "2011-03-30T14:25:56Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![BUroKHUli](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/burokhuli/32/190_2.png) [@BUroKHUli](https://forum.kirupa.com/u/BUroKHUli)\
**Post date:** [March 30, 2011, 2:25pm UTC](https://forum.kirupa.com/t/free-email-form-php/320691/1 "2011-03-30T14:25:56Z")

</div>

Hi guys,

I’ve only been practicing PHP for a week now but I was searching for email forms that I can use for my personal website and found this one. The thing I like about this code is it’s VALIDATION. But I think something is weird in a lot of way because the original code would say that the NAME, LASTNAME, EMAIL are INVALID even if I entered a correct one. Can someone explain to me why this is happening?

\<?php  
if(isset($\_POST[‘email’])) {

```
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "you@yourdomain.com";
$email_subject = "Your email subject line";
 
 
function died($error) {
    // your error code can go here
    echo "We are very sorry, but there were error(s) found with the form you submitted. ";
    echo "These errors appear below.&lt;br /&gt;&lt;br /&gt;";
    echo $error."&lt;br /&gt;&lt;br /&gt;";
    echo "Please go back and fix these errors.&lt;br /&gt;&lt;br /&gt;";
    die();
}
 
// validation expected data exists
if(!isset($_POST['first_name']) ||
    !isset($_POST['last_name']) ||
    !isset($_POST['email']) ||
    !isset($_POST['telephone']) ||
    !isset($_POST['comments'])) {
    died('We are sorry, but there appears to be a problem with the form you submitted.');       
}
 
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_POST['comments']; // required
 
$error_message = "";
$email_exp = "/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/";

```

if(!preg\_match($email\_exp,$email\_from)) {  
$error\_message .= ‘The Email Address you entered does not appear to be valid.\<br /\>’;  
}  
$string\_exp = “/^[a-z .’-]+$/”;  
if(!preg\_match($string\_exp,$first\_name)) {  
$error\_message .= ‘The First Name you entered does not appear to be valid.\<br /\>’;  
}  
if(!preg\_match($string\_exp,$last\_name)) {  
$error\_message .= ‘The Last Name you entered does not appear to be valid.\<br /\>’;  
}  
if(strlen($comments) \< 2) {  
$error\_message .= ‘The Comments you entered do not appear to be valid.\<br /\>’;  
}  
if(strlen($error\_message) \> 0) {  
died($error\_message);  
}  
$email\_message = "Form details below.

";

```
function clean_string($string) {
  $bad = array("content-type","bcc:","to:","cc:","href");
  return str_replace($bad,"",$string);
}
 
$email_message .= "First Name: ".clean_string($first_name)."

```

";  
$email\_message .= "Last Name: “.clean\_string($last\_name).”  
";  
$email\_message .= "Email: “.clean\_string($email\_from).”  
";  
$email\_message .= "Telephone: “.clean\_string($telephone).”  
";  
$email\_message .= "Comments: “.clean\_string($comments).”  
";

// create email headers  
$headers = 'From: '.$email\_from."  
“.  
'Reply-To: '.$email\_from.”  
" .  
‘X-Mailer: PHP/’ . phpversion();  
@mail($email\_to, $email\_subject, $email\_message, $headers);  
?\>

\<!-- include your own success html here --\>

Thank you for contacting us. We will be in touch with you very soon.

\<?php  
}  
?\>
