Advertisement
gitlez

YA: Contact Form Processing #2

Apr 12th, 2012
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.83 KB | None | 0 0
  1. <?php
  2. // In response to question on Yahoo Answer's
  3.  
  4. /*    Functions    */
  5. function validateEmail($i){
  6.     //http://www.iamcal.com/publish/articles/php/parsing_email/
  7.     $qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
  8.     $dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
  9.     $atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c'.
  10.         '\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
  11.     $quoted_pair = '\\x5c[\\x00-\\x7f]';
  12.     $domain_literal = "\\x5b($dtext|$quoted_pair)*\\x5d";
  13.     $quoted_string = "\\x22($qtext|$quoted_pair)*\\x22";
  14.     $domain_ref = $atom;
  15.     $sub_domain = "($domain_ref|$domain_literal)";
  16.     $word = "($atom|$quoted_string)";
  17.     $domain = "$sub_domain(\\x2e$sub_domain)*";
  18.     $local_part = "$word(\\x2e$word)*";
  19.     $addr_spec = "$local_part\\x40$domain";
  20.     return (strrpos($i,'.') > strrpos($i,'@'))? preg_match("!^$addr_spec$!", $i) : false;
  21. }
  22. function rfv(){  // Required Form Inputs
  23.     $a = func_get_args();
  24.     $cn = func_num_args();
  25.     for($i=0; $i<$cn; ++$i){
  26.         if(!isset($_POST[$a[$i]]{0})){
  27.             return false;
  28.         }
  29.     }
  30. }
  31. function died($error) {
  32.     // your error code can go here
  33.     echo "We are very sorry, but there were error(s) found with the form you submitted. ";
  34.     echo "These errors appear below.<br><br>";
  35.     echo $error."<br><br>";
  36.     echo "Please go back and fix these errors.";
  37.     exit;
  38. }
  39. function clean_string($string) {
  40.     $bad = array("content-type","bcc:","to:","cc:","\r\n","\r","\n");
  41.     return str_replace($bad,"",$string);
  42. }
  43.  
  44.  
  45.  
  46. /*    Start of the Form Processing    */
  47. if(isset($_POST['email'])) {
  48.  
  49.     // CHANGE THE TWO LINES BELOW
  50.     $email_to = "sales@thebusinessupgrades.com";
  51.     $email_subject = "Order Forms";
  52.  
  53.  
  54.     $_POST = array_map('trim', $_POST);
  55.  
  56.  
  57.  
  58.     // validation expected data exists
  59.     if( !rfv('first_name','last_name','email','Name of Company','Colors','Target Audience','comments')) { // Required Form Inputs
  60.         died('We are sorry, but there appears to be some missing form fields.');
  61.     }
  62.  
  63.     $first_name = $_POST['first_name']; // required
  64.     $last_name = $_POST['last_name']; // required
  65.     $email_from = $_POST['email']; // required
  66.     $telephone = $_POST['telephone']; // not required
  67.     $name_of_company = $_POST['name_of_company']; // required
  68.     $Colors = $_POST['Colors']; // required
  69.     $target_audience = $_POST['target_audience']; // required
  70.     $comments = $_POST['comments']; // required
  71.  
  72.     /*    Error Checking    */
  73.     $error_message = "";
  74.  
  75.     if(!validateEmail($email_from)) {
  76.         $error_message .= 'The Email Address you entered does not appear to be valid.<br>';
  77.     }
  78.     if(!preg_match( "/^[A-Za-z .'-]+$/", $first_name . ' ' . $last_name)) {
  79.         $error_message .= 'The First or Last Name you entered appears to contain invalid characters.<br>'; // Althought this will fail against "foreign" names
  80.     }
  81.     if(!isset($comments{2})) {
  82.         $error_message .= 'The Comments you entered do not appear to be valid.<br>';
  83.     }
  84.     if(strlen($error_message) > 0) {
  85.         died($error_message);
  86.     }
  87.    
  88.     $email_message = "Form details below.\n\n";
  89.     $email_message .= "First Name: ".clean_string($first_name)."\n";
  90.     $email_message .= "Last Name: ".clean_string($last_name)."\n";
  91.     $email_message .= "Email: ".clean_string($email_from)."\n";
  92.     $email_message .= "Telephone: ".clean_string($telephone)."\n";
  93.     $email_message .= "Name of Company: ".clean_string($name_of_company)."\n";
  94.     $email_message .= "Colors: ".clean_string($Colors)."\n";
  95.     $email_message .= "target_audience: ".clean_string($target_audience)."\n";
  96.     $email_message .= "Comments: ".clean_string($comments)."\n";
  97.  
  98.  
  99.     // create email headers
  100.     $headers = 'From: ' . $email_from . "\r\n";
  101.     'Reply-To: ' . $email_from . "\r\n" .
  102.     'X-Mailer: PHP/' . phpversion();
  103.     if(!@mail($email_to, $email_subject, $email_message, $headers)){
  104.         die('The mail could not be sent due to an internal error. Please try again later.');
  105.     }
  106. }
  107. ?>
  108.  
  109. <!-- place your own success html below -->
  110.  
  111. Thank you for for choosing the Business Upgrades!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement