PHP Contact Form

By Zen on Jul 17, 2008

Simple php contact form for your web site

<?php

define("MAIL_TARGET","youremail@domain.com");

define("errorName","Invalid name! It must be at least 2 characters long");
define("errorEmail","Invalid email address!");
define("errorMsg","Invalid message! It must be at least 10 characters long");

function createForm($subject="",$name="",$email="",$message="",$error1="",$error2="",$error3=""){
?>
      <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
        <table>
          <tr><td>Subject:</td><td></td></tr>
          <tr><td colspan="2"><input type="text" name="subject" value="<?php echo $subject; ?>"></td></tr>
          <tr><td>Name: </td><td><?php echo $error1; ?></td></tr>
          <tr><td colspan="2"><input type="text" name="name" value="<?php echo $name; ?>"></td></tr>
          <tr><td>Email:</td><td><?php echo $error2; ?></td></tr>
          <tr><td colspan="2"><input type="text" name="email" value="<?php echo $email; ?>"></td></tr>
          <tr><td>Message:</td><td><?php echo $error3; ?></td></tr>
          <tr><td colspan="2"><textarea cols="40" rows="6" name="message"><?php echo $message; ?></textarea></td></tr>
          <tr><td colspan="2"><br/><input type="submit" name="submitBtn" value="Send"></td></tr>
        </table>
      </form>
<?php
}

function isValidEmail($email){
   $pattern = "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$";

   if (eregi($pattern, $email)){
      return true;
   }
   else {
      return false;
   }   
}

function sendMail($name,$email,$message,$subject){

    $subject = "Message from website: $subject";
    $from    = "From: $name <$email>\r\nReply-To: $email\r\n"; 
    $header  = "MIME-Version: 1.0\r\n"."Content-type: text/html; charset=iso-8859-1\r\n";
    $content = htmlspecialchars($message);

    $content = wordwrap($content,70);
    @mail(MAIL_TARGET,$subject,$content,$from.$header);

}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html>
<body>
<?php if (!isset($_POST['submitBtn']))  {
    createForm();
} else  {
      $subject = isset($_POST['subject']) ? $_POST['subject'] : "";
      $name    = isset($_POST['name'])    ? $_POST['name'] : "";
      $email   = isset($_POST['email'])   ? $_POST['email'] : "";
      $message = isset($_POST['message']) ? $_POST['message'] : "";

      $error  = false;
      $error1 = '';
      $error2 = '';
      $error3 = '';

      if (strlen($name)<2) {
          $error = true;
          $error1 = errorName;
      }
      if (!isValidEmail($email)) {
          $error = true;
          $error2 = errorEmail;
      }
      if (strlen($message)<10) { 
          $error = true;
          $error3 = errorMsg;
      }

      if ($error){
         createForm($subject,$name,$email,$message,$error1,$error2,$error3);
      }
      else {
          sendMail($name,$email,$message,$subject);
    ?>

        <table width="100%">
          <tr><td>
            Thanks for your message!
          </td></tr>
        </table>
<?php
    }
}
?>
</body>

Comments

Sign in to comment.
gooshie   -  Feb 11, 2010

nice

 Respond  
ksenthan   -  Nov 15, 2009

Really helpful code... Thanks for that.

 Respond  
Hawkee   -  Dec 20, 2008

Good point jcbones, honeypots are quite clever.

 Respond  
jcbones   -  Dec 19, 2008

I don't like CAPTCHA as it makes my user's work more so that I can get away from the spammers. Then you have the whole color blind thing too.

I prefer honeypots. If I get spam on that, I'll just change it up a little so he has to remake his script...

 Respond  
Zen   -  Jul 20, 2008

Yep. Ill add captcha for 1 week... Im too busy now

 Respond  
Hawkee   -  Jul 18, 2008

Nice, just needs a CAPTCHA now.

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.