Sending form data to email via php

The below is a simple script that handles sending form data to email using php:

<?php

   // checks to see if the page that called this script was sent from the same host
   if ($_SERVER['REQUEST_METHOD']=="POST"){
      if (strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST'])>7 ||
         !strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST']))
         die("Bad referer");

      // begin building the message body
      $msg="Values submitted by the user:\n";
      foreach($_POST as $key => $val){
         if (is_array($val)){
            $msg.="Item: $key\n";
            foreach($val as $v){
               $v = stripslashes($v);
               $msg.="   $v\n";
            }
         } else {
            $val = stripslashes($val);
            $msg.="$key: $val\n";
         }
      }

      // assign recipient and subject
      $recipient="me@domain_name.tld";
      $subject="Form submission";

      // turn off all error reporting
      error_reporting(0);

      // send mail
      if (mail($recipient, $subject, $msg)){
         echo "<h1>Thank you</h1><p>Message successfully sent:</p>\n";
         echo nl2br($msg);
      } else
         echo "An error occurred and the message could not be sent.";
   } else
      echo "Bad request method";
?>

Save the above script as processForm.php and call it via the action attribute:

   <form action="processForm.php" method="post">
Comment