Advertisement
Guest User

ojetecalor

a guest
Nov 28th, 2014
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.04 KB | None | 0 0
  1. <?php
  2. if($_POST) {
  3.  
  4.     $to_Email = "juan.koan@gmail.com"; //Replace with recipient email address
  5.    
  6.     //check if its an ajax request, exit if not
  7.     if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
  8.    
  9.         //exit script outputting json data
  10.         $output = json_encode(
  11.         array(
  12.             'type'=> 'error',
  13.             'text' => 'Request must come from Ajax'
  14.         ));
  15.        
  16.         die($output);
  17.     }
  18.    
  19.     //check $_POST vars are set, exit if any missing
  20.     if(!isset($_POST["userName"]) || !isset($_POST["userEmail"]) || !isset($_POST["userSubject"]) || !isset($_POST["userMessage"])) {
  21.         $output = json_encode(array('type'=>'error', 'text' => 'Input fields are empty!'));
  22.         die($output);
  23.     }
  24.    
  25.     //additional php validation
  26.     if(empty($_POST["userName"])) {
  27.         $output = json_encode(array('type'=>'error', 'text' => 'Name is too short or empty!'));
  28.         die($output);
  29.     }
  30.     if(!filter_var($_POST["userEmail"], FILTER_VALIDATE_EMAIL)) {
  31.         $output = json_encode(array('type'=>'error', 'text' => 'Please enter a valid email!'));
  32.         die($output);
  33.     }
  34.     if(strlen($_POST["userMessage"])<5) {
  35.         $output = json_encode(array('type'=>'error', 'text' => 'Too short message! Please enter something.'));
  36.         die($output);
  37.     }
  38.    
  39.     //proceed with PHP email.
  40.     $headers = 'From: '.$_POST["userEmail"].'' . "\r\n" .
  41.     'Reply-To: '.$_POST["userEmail"].'' . "\r\n" .
  42.     'X-Mailer: PHP/' . phpversion();
  43.    
  44.         // send mail
  45.     $sentMail = @mail($to_Email, $_POST["userSubject"], $_POST["userMessage"] .'  -'.$_POST["userName"], $headers);
  46.    
  47.     if(!$sentMail) {
  48.         $output = json_encode(array('type'=>'error', 'text' => 'Could not send mail! Please check your PHP mail configuration.'));
  49.         die($output);
  50.     } else {
  51.         $output = json_encode(array('type'=>'message', 'text' => 'Hi '.$_POST["userName"] .' Thank you for your email'));
  52.         die($output);
  53.     }
  54. }
  55. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement