title: Google Recaptcha v2 With email form, gives http 500 Error
body: <p>Using an html form for a "contact us". This passes name, email, &amp; message to a .php script and it works well. Add the Google recaptua v2 to this form gives a http 500 Error. This post and the code have been edited to reflect the KaplanKomputing tutorial suggested by Chris White.</p>
  
  <p>You can visit the working form without recaptcha, and nonworking recaptcha here:
  <a href="https://coinsandhistory.com#contact" rel="nofollow noreferrer">https://coinsandhistory.com#contact</a></p>
  
  <p>The "Google site key" I'll call here "XXXX-Google-site" and "YYYY-Google-secret".</p>
  
  <p>1st the contact form html, you don't need the css styling nor the stripslashes from the tutorial.</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;script src="https://www.google.com/recaptcha/api.js" async defer&gt; 
  &lt;/script&gt;
  
  &lt;link rel="stylesheet" href="../css/send-mail.css"&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  &lt;!-- https://stackoverflow.com/questions/27188436/html-php-contact-form- 
  email/55962553 --&gt;
  &lt;!-- https://kaplankomputing.com/blog/tutorials/
  recaptcha-php-demo-tutorial/ --&gt;
  &lt;form action="send-mail_SO2_recapt.php" method="post" 
  enctype="multipart/form-data" name="myemailform"&gt;
  &lt;div&gt;
  &lt;span&gt;Name &amp;nbsp;&lt;/span&gt;
  &lt;input type="text" name="name" value="" placeholder="Your Name"&gt;
  &lt;/div&gt;
  &lt;div&gt;
  &lt;span&gt;Email &amp;nbsp;&lt;/span&gt;
  &lt;input type="email" name="web_email" autocapitalize="off" 
  autocorrect="off" 
  value="" placeholder="youremail@domain.com"&gt;
  &lt;/div&gt;
  
  &lt;div&gt;
  &lt;span&gt;messgae &amp;nbsp;&lt;/span&gt;
  &lt;textarea name="message" placeholder="message"&gt;&lt;/textarea&gt;
  &lt;/div&gt;
  
  &lt;!--  Google v2 Recaptua Form   --&gt;
  &lt;div class="g-recaptcha" data-sitekey="XXXX-Google-site"&gt;&lt;/div&gt;
  &lt;br/&gt;
  
  &lt;div class="code"&gt;
  &lt;button&gt;&lt;input type="submit" name="submit" value="Send"&gt;&lt;/button&gt;
  &lt;/div&gt;
  &lt;i class="clear" style="display: block"&gt;&lt;/i&gt;
  &lt;/div&gt;
  &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>And then the send-mail.php script. I called mine "send-mail_SO2_recapt.php".</p>
  
  <pre><code>&lt;?php
  /* error reporting, should rmv from working form */
  error_reporting(E_ALL);
  ini_set('display_errors', 1);
  
  if(!isset($_POST['submit']))
  {
  //This page should not be accessed directly. Need to submit the form.
  echo "error; you need to submit the form!";
  }
  $name = $_POST["name"];
  $visitor_email = $_POST['web_email'];
  $message = $_POST["message"];
  $response = $_POST["g-recaptcha-response"];
  
  //Validate first
  if(empty($name)||empty($visitor_email))
  {
  echo "Name and email are needed!";
  exit;
  }
  if(IsInjected($visitor_email))
  {
  echo "Bad email value!";
  exit;
  }
  
  $url = "https://google.com/recaptcha/api/siteverify";
  $data = array(
  "secret" =&gt; "YYYY-Google-secret",
  "response" =&gt; $_POST["g-recaptcha-response"]);
  $options = array(
  "https" =&gt; array (
  "method" =&gt; "POST",
  "content" =&gt; https_build_query($data)
  )
  );
  $context  = stream_context_create($options);
  $verify = file_get_contents($url, false, $context);
  $captcha_success=json_decode($verify);
  
  if ($captcha_success=&gt;success==false) {
  echo "&lt;p&gt;You are a bot! Go away!&lt;/p&gt;"; }
  else if ($captcha_success=&gt;success==true) {
  echo "&lt;p&gt;You are not not a bot!&lt;/p&gt;";   }
  
  // $email_from = 'info@coinsandhistory.com';//&lt;== update the email address
  $email_from = "$visitor_email";
  $email_subject = "New Form submission";
  $email_body = "You have received a new message from $name.\n".
  "sender's email:\n $email_from\n".
  "Here is the message:\n $message";
  
  $to = "youremail@yourdomain.com";   //&lt;== update the email address
  $headers = "From: $email_from \r\n";
  $headers .= "Reply-To: $visitor_email \r\n";
  //Send the email!
  mail($to,$email_subject,$email_body,$headers);
  //done. redirect to thank-you page.
  header('Location: thank_you_SO2.html');
  exit;
  
  // Function to validate against any email injection attempts
  ?&gt;
  </code></pre>
  
  <p>If you supply code samples, please indicate what form it is: eg html, php, javascript.  I can't believe I'm the 1st person to try to use a simple Google recaptua in a contact form but this question doesn't appear plainly anywhere.</p>