Creating the form Processor php script

There are some things to consider when creating this script. Is someone visiting the script directly, not as a result of the form submission, is there any post data and whether any of that post data is blank are some things we need to consider.

Lets start out our code and set up some necessary variables.

File: formProcessor.php 
<?php 
  // get the post content 
  $post = $_POST; 

  // if no post data, exit! 
  if (!$post) 
    exit; 

  // get the name of the referring page 
  $referer = $_SERVER['HTTP_REFERER']; 

  // enter the address to send email to 
  $sendTo = 'me@myemailaddress.com'; 

  // set the page to send user after message has been sent 
  // can be relative path to file or URL 
  $returnTo = 'thanks.html'; 

  // array to hold errors 
  $errors = array(); 

  // Start the message off 
  $message = 'You have received a messaged from web page ' . $referer . "\r\n"; 

// other code

The first thing we do is to grab the post content. "$_POST" is a predefined PHP variable that holds any post content contained in the HTTP request. I like to store it in a variable that is easier for me to type than $_POST. If there is any $_POST content it will now be stored in this variable, otherwise the variable will be empty, or null.

Now we do a simple check to see if there is any post content. If a person tried to access this script directly, there wouldn't be any, which would mean undefined variables further down the page. To prevent this we simply "exit" the execution of the script.

If we make it past that check we set up some variables. First thing we do is grab the referring page from the pre-defined PHP $_SERVER array. The array key "HTTP_REFERER" will hold the url of the page that sent the request to this page.

We then set the email address we want the form information sent to. This is where you enter "your" email address, as we will use it in PHP's mail() function soon.

Next we create a variable to hold the location we want to send the user to after the form is processed. It's nice to display some sort of "thank you" page to the user to let them now that there form has been processed.

We create an empty array to hold any errors we might encounter. This will be used to check for empty fields, which will be our only validation check. We will later check if this array is still empty to decide whether to move forward with sending the email or redirect the user back to the form.

Finally, we need a container to hold our message that we will be creating from the form post data. We start it off with a message to inform the email recipient, in this case you, of where this email came from. The "\r\n" characters are "carriage return/ newline" characters. They will tell the email to start a new line when being displayed. This is like "
" in HTML.

Now lets create the logic to process the form.

File: formProcessor.php (cont)
// other code... 

// check that there was post data 
if ($post) { 
  foreach ($post as $k => $v) { 
    // check for blank feilds and add to errors array 
    if (strlen(trim($v)) == 0) { 
      $errors[] = $k; continue; 
    } 

    // add post data except "submit" to the message 
    if ($k != 'submit') 
      $message .= ucfirst($k) . ": \r\n" . $v . "\r\n\r\n"; 
  } 


// other code...

Here we are again verifying that there is post data. We then break the post data into key and value pairs, which contains the form field name and the value entered by the user. We do a check on the value to make sure that, with all whitespace trimmed, there is some content in each field. If there isn't, we add the field name to the errors array.

After that we add all the posted info except the submit button to the message. We give the forms field name a capitalized first letter with PHP's ucfirst() function and use some newlines to format the content for the email a bit.

Now lets look at how to send this crazy email!

// other code... 

// check for errors 
if (count($errors) == 0) { 
  // no errors found, set required mail() parameters 
  $subject = 'Form submitted from: ' . $referer; 
  $headers = 'From: ' . $_SERVER['SERVER_ADMIN'] . "\r\n" . 
    'Reply-To: ' . $_SERVER['SERVER_ADMIN'] . "\r\n" . 
    'X-Mailer: PHP/' . phpversion(); 

  // make sure each line length not over 70 chars 
  $message = wordwrap($message, 70); 

  // try to send mail and then redirect 
  if (mail($sendTo, $subject, $message, $headers)) { 
    header('Location: '. $returnTo); 
    exit; 
  } else { 
    header('Location: '. $referer); 
    exit; 
  } 

} else { 
  // There were blank fields, return to form 
  header('Location: '. $referer); 
  exit; 

?>

Here we close out the formProccessor.php script. We do a count check on the errors array, redirect the user to the form using the header() function if we find any errors (blank fields) or process the email if there aren't any errors. We set the required variables for the mail function. The subject will state which form it came from, and the headers will set the "From:" email address to the value set in our server environment, as well as inform the email client that the email was sent by PHP. We then wordwrap the message content to make everything fit nicely in our email browser window.

Next we try to send the email using PHP's mail() function, if it is successful we redirect the user to the $returnTo page. If the mail() function returned a false response we return the user to the form as a default.

The only thing left to do is create that "thank you" page.