Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2015
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.37 KB | None | 0 0
  1. <?php
  2.   /**
  3.    * Sets error header and json error message response.
  4.    *
  5.    * @param  String $messsage error message of response
  6.    * @return void
  7.    */
  8.      session_start();
  9.   function errorResponse ($messsage) {
  10.     header('HTTP/1.1 500 Internal Server Error');
  11.     die(json_encode(array('message' => $messsage)));
  12.   }
  13.  
  14.   /**
  15.    * Pulls posted values for all fields in $fields_req array.
  16.    * If a required field does not have a value, an error response is given.
  17.    */
  18.   function constructMessageBody () {
  19.     $fields_req =  array("name" => true, "email" => true, "message" => true);
  20.     $message_body = "";
  21.     foreach ($fields_req as $name => $required) {
  22.       $postedValue = $_POST[$name];
  23.       if ($required && empty($postedValue)) {
  24.         errorResponse("$name is empty.");
  25.       } else {
  26.         $message_body .= ucfirst($name) . ":  " . $postedValue . "\n";
  27.       }
  28.     }
  29.     return $message_body;
  30.   }
  31.  
  32.   header('Content-type: application/json');
  33.  
  34.   //do Captcha check, make sure the submitter is not a robot:)...
  35.   $url = 'https://www.google.com/recaptcha/api/siteverify';
  36.   $opts = array('http' =>
  37.     array(
  38.       'method'  => 'POST',
  39.       'header'  => 'Content-type: application/x-www-form-urlencoded',
  40.       'content' => http_build_query(array('secret' => 'secret', 'response' => $_POST["g-recaptcha-response"]))
  41.     )
  42.   );
  43.   $context  = stream_context_create($opts);
  44.   $result = json_decode(file_get_contents($url, false, $context, -1, 40000));
  45.  
  46.   if (!$result->success) {
  47.     errorResponse('reCAPTCHA checked failed!');
  48.   }
  49.   //attempt to send email
  50.   $messageBody = constructMessageBody();
  51.   require 'vender/php_mailer/PHPMailerAutoload.php';
  52.   $mail = new PHPMailer;
  53.   $mail->CharSet = 'UTF-8';
  54.   $mail->isSMTP();
  55.    $mail->Host = "smtp.gmail.com";
  56.   $mail->SMTPAuth = true;
  57.   $mail->Username = "info.domdit@gmail.com";
  58.   $mail->Password = "secret";
  59.   }
  60.  
  61.   $mail->SMTPSecure = 'tls';
  62.   $mail->Port = 587;
  63.  
  64.   $mail->setFrom($_POST['email'], $_POST['name']);
  65. $mail->addAddress("me@gmail.com");
  66.  
  67.   $mail->Subject = $_POST['reason'];
  68.   $mail->Body  = $messageBody;
  69.  
  70.  
  71.   //try to send the message
  72.   if($mail->send()) {
  73.     echo json_encode(array('message' => 'Your message was successfully submitted.'));
  74.   } else {
  75.     errorResponse('An expected error occured while attempting to send the email: ' . $mail->ErrorInfo);
  76.   }
  77. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement