Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- script simplification for:
- http://answers.yahoo.com/question/?qid=20110617205843AAeDwtm
- */
- define('BACK_MSG', ' Please hit your browser back button and <a href="Javascript: history.go(-1);">try again</a>.');
- /*
- * FUNCTIONS
- */
- function requiredField($name,$msg){
- if(strlen($_REQUEST[$name]) === 0){
- die($msg);
- }
- }
- function checkemail($field) {
- // checks proper syntax
- if( !preg_match( "/^([a-zA-Z0-9])+([a-zA-Z0-9._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9._-]+)+$/", $field)){
- die("Improper email address detected." . BACK_MSG);
- }
- return $field;
- }
- function spamcheck($field){
- // eregi is depricated, as well as resource heavy compared to str functions. Each has their place, this is not eregi's place.
- $spamMarkers = Array("to:","cc:","bcc:","\r","\n","%0A");
- $c = count($spamMarkers);
- $lowered = strtolower($field);
- for($i = 0; $i < $c; ++$i){
- if(strpos($lowered,$spamMarkers[$i]) !== false){
- // No need to set $possiblespam, or return 1, as Script die's
- die("Possible spam attempt detected. If this is not the case, please edit the content of the contact form and try again." . BACK_MSG);
- }
- }
- return $field;
- }
- function strlencheck($field, $minlength, $whichfieldresponse){
- if (strlen($field) < $minlength){
- die($whichfieldresponse);
- }
- }
- /*
- * REQUIRED INPUT CHECKS
- */
- requiredField('subject',"You did not choose a subject line." . BACK_MSG);
- requiredField('emailaddress',"You did not choose a recipient." . BACK_MSG);
- /*
- * VARIABLES
- */
- $sentPage = 'email_sent.php'; // Page that the user will be sent to on a successfull email send
- $notSentPage = 'email_not_sent.php'; // Page that the user will be sent to if there was an error with the sending of the email.
- $email = spamcheck(checkemail($_REQUEST['email']));
- $message = $_REQUEST['message'];
- $subject = Array(
- '1' => "Membership/Burgees",
- '2' => "Submitting content (articles/pictures etc...)",
- '3' => "Voyage planning",
- '4' => "Suggestions",
- '5' => "Other");
- $subject = $subject[$_REQUEST['subject']];
- $emailaddress = Array(
- $emailaddress = $emailaddress[$_REQUEST['emailaddress']];
- /*
- * INPUT CHECKING
- */
- strlencheck($email, 10, "The email address field is too short." . BACK_MSG);
- strlencheck($subject, 1, "You did not choose a subject." . BACK_MSG);
- strlencheck($message, 10, "The message field is too short." . BACK_MSG);
- strlencheck($emailaddress, 8, "You have not selected a recipient of your message." . BACK_MSG);
- /*
- * EMAIL SENDING
- */
- // 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.
- if(mail($emailaddress,"Subject: {$subject}",$message,"From: {$email}" )){
- // Mail sent successfully
- header("Location: {$sentPage}");
- exit;
- }else{
- header("Location: {$notSentPage}");
- exit;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment