gitlez

YA: Contact Form Processing

Jun 18th, 2011
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.09 KB | None | 0 0
  1. <?php
  2. /*
  3.     script simplification for:
  4.     http://answers.yahoo.com/question/?qid=20110617205843AAeDwtm
  5. */
  6.  
  7. define('BACK_MSG', ' Please hit your browser back button and <a href="Javascript: history.go(-1);">try again</a>.');
  8.  
  9. /*
  10. *            FUNCTIONS
  11. */
  12. function requiredField($name,$msg){
  13.     if(strlen($_REQUEST[$name]) === 0){
  14.         die($msg);
  15.     }
  16. }
  17. function checkemail($field) {
  18.     // checks proper syntax
  19.     if( !preg_match( "/^([a-zA-Z0-9])+([a-zA-Z0-9._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9._-]+)+$/", $field)){
  20.         die("Improper email address detected." . BACK_MSG);
  21.     }
  22.     return $field;
  23. }
  24. function spamcheck($field){
  25.     // eregi is depricated, as well as resource heavy compared to str functions. Each has their place, this is not eregi's place.
  26.     $spamMarkers = Array("to:","cc:","bcc:","\r","\n","%0A");
  27.     $c = count($spamMarkers);
  28.     $lowered = strtolower($field);
  29.     for($i = 0; $i < $c; ++$i){
  30.         if(strpos($lowered,$spamMarkers[$i]) !== false){
  31.             // No need to set $possiblespam, or return 1,  as Script die's
  32.             die("Possible spam attempt detected. If this is not the case, please edit the content of the contact form and try again." . BACK_MSG);
  33.         }
  34.     }
  35.     return $field;
  36. }
  37. function strlencheck($field, $minlength, $whichfieldresponse){
  38.     if (strlen($field) < $minlength){
  39.         die($whichfieldresponse);
  40.     }
  41. }
  42.  
  43. /*
  44. *            REQUIRED INPUT CHECKS
  45. */
  46. requiredField('subject',"You did not choose a subject line." . BACK_MSG);
  47. requiredField('emailaddress',"You did not choose a recipient." . BACK_MSG);
  48.  
  49. /*
  50. *            VARIABLES
  51. */
  52. $sentPage = 'email_sent.php'; // Page that the user will be sent to on a successfull email send
  53. $notSentPage = 'email_not_sent.php'; // Page that the user will be sent to if there was an error with the sending of the email.
  54. $email = spamcheck(checkemail($_REQUEST['email']));
  55. $message = $_REQUEST['message'];
  56.  
  57. $subject = Array(
  58.     '1' => "Membership/Burgees",
  59.     '2' => "Submitting content (articles/pictures etc...)",
  60.     '3' => "Voyage planning",
  61.     '4' => "Suggestions",
  62.     '5' => "Other");
  63. $subject = $subject[$_REQUEST['subject']];
  64.  
  65. $emailaddress = Array(
  66.     '1' => "[email protected]",
  67.     '2' => "[email protected]",
  68.     '3' => "[email protected]");
  69. $emailaddress = $emailaddress[$_REQUEST['emailaddress']];
  70.  
  71. /*
  72. *            INPUT CHECKING
  73. */
  74. strlencheck($email, 10, "The email address field is too short." . BACK_MSG);
  75. strlencheck($subject, 1, "You did not choose a subject." . BACK_MSG);
  76. strlencheck($message, 10, "The message field is too short." . BACK_MSG);
  77. strlencheck($emailaddress, 8, "You have not selected a recipient of your message." . BACK_MSG);
  78.  
  79. /*
  80. *            EMAIL SENDING
  81. */
  82.  
  83.  
  84. // All Checks will stop the script if there is an error. So no need to check for errors when the script will die() on errors.
  85. if(mail($emailaddress,"Subject: {$subject}",$message,"From: {$email}" )){
  86.     // Mail sent successfully
  87.     header("Location: {$sentPage}");
  88.     exit;
  89. }else{
  90.     header("Location: {$notSentPage}");
  91.     exit;
  92. }
  93. ?>
Advertisement
Add Comment
Please, Sign In to add comment