Advertisement
Guest User

Untitled

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