title: Form with reCaptcha - PHP code sends mail locally but not on remote server
body: <p>I have created a form from a tutorial here (<a href="https://bootstrapious.com/p/bootstrap-recaptcha" rel="nofollow noreferrer">https://bootstrapious.com/p/bootstrap-recaptcha</a>). I am using MAMP and have set up a testing mail server using PHP on my Mac.</p>
  
  <p>The PHP code sends my form data perfectly on my local machine but when I upload it to my server (which also has other sites with forms using PHP that send with no problem) the form data doesn't send. (I have replaced the reCaptcha secret and site-key with words).</p>
  
  <h2>HTML</h2>
  
  <pre><code>&lt;form id="contact-form" action="process.php" method="post"&gt;
              &lt;div class="form-heading"&gt;Please Contact Us&lt;/div&gt;
  
              &lt;div class="messages"&gt;&lt;/div&gt;
  
              &lt;div class="col-md-8"&gt;
                  &lt;div class="form-group"&gt;
                      &lt;label for="form_name"&gt;Name *&lt;/label&gt;
                      &lt;input id="form_name" type="text" name="name" class="form-control" placeholder="Please enter your name *" required="required" data-error="Name is required."&gt;
                      &lt;div class="help-block with-errors"&gt;&lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
  
              &lt;div class="col-md-8"&gt;
                  &lt;div class="form-group"&gt;
                      &lt;label for="form_email"&gt;Email *&lt;/label&gt;
                      &lt;input id="form_email" type="email" name="email" class="form-control" placeholder="Please enter your email *" required="required" data-error="Valid email is required."&gt;
                      &lt;div class="help-block with-errors"&gt;&lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
  
              &lt;div class="col-md-8"&gt;
                  &lt;div class="form-group"&gt;
                      &lt;label for="form_phone"&gt;Phone&lt;/label&gt;
                      &lt;input id="form_phone" type="tel" name="phone" class="form-control" placeholder="Please enter your phone"&gt;
                      &lt;div class="help-block with-errors"&gt;&lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
  
              &lt;div class="col-md-12"&gt;
                  &lt;div class="form-group"&gt;
                      &lt;label for="form_message"&gt;Message *&lt;/label&gt;
                      &lt;textarea id="form_message" name="message" class="form-control" placeholder="What's on your mind? *" rows="4" required="required" data-error="Please leave us a message."&gt;&lt;/textarea&gt;
                      &lt;div class="help-block with-errors"&gt;&lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
  
              &lt;div class="g-recaptcha" data-sitekey="SITE KEY HERE"&gt;&lt;/div&gt;
  
              &lt;input id="submit-btn" type="submit" value="Submit" /&gt;
          &lt;/form&gt;
  </code></pre>
  
  <h2>AJAX</h2>
  
  <pre><code>$(function () {
  
  // init the validator
  // validator files are included in the download package
  // otherwise download from http://1000hz.github.io/bootstrap-validator
  
  $('#contact-form').validator();
  
  
  // when the form is submitted
  $('#contact-form').on('submit', function (e) {
  
      // if the validator does not prevent form submit
      if (!e.isDefaultPrevented()) {
          var url = "process.php";
  
          // POST values in the background the the script URL
          $.ajax({
              type: "POST",
              url: url,
              data: $(this).serialize(),
              success: function (data)
              {
                  // data = JSON object that contact.php returns
  
                  // we recieve the type of the message: success x danger and apply it to the 
                  var messageAlert = 'alert-' + data.type;
                  var messageText = data.message;
  
                  // let's compose Bootstrap alert box HTML
                  var alertBox = '&lt;div class="alert ' + messageAlert + ' alert-dismissable"&gt;&lt;button type="button" class="close" data-dismiss="alert" aria-hidden="true"&gt;&amp;times;&lt;/button&gt;' + messageText + '&lt;/div&gt;';
  
                  // If we have messageAlert and messageText
                  if (messageAlert &amp;&amp; messageText) {
                      // inject the alert to .messages div in our form
                      $('#contact-form').find('.messages').html(alertBox);
                      // empty the form
                      $('#contact-form')[0].reset();
                      // reset the ReCaptcha
                      grecaptcha.reset();
                  }
              }
          });
          return false;
      }
  });
  });
  </code></pre>
  
  <h2>PHP</h2>
  
  <pre><code>&lt;?php
  
  // require ReCaptcha class
  require('recaptcha-master/src/autoload.php');
  
  // configure
  $from = 'example@here.com&gt;';
  $sendTo = 'example@here.com&gt;';
  $subject = 'New message from website contact form';
  $fields = array('name' =&gt; 'Name', 'email' =&gt; 'Email', 'phone' =&gt; 'Phone', 'message' =&gt; 'Message'); // array variable name =&gt; Text to appear in the email
  $okMessage = 'Contact form successfully submitted. Thank you, we will get back to you as soon as possible!';
  $errorMessage = 'There was an error while submitting the form. Please try again later';
  $recaptchaSecret = 'SECRET KEY HERE';
  
  // let's do the sending
  
  try
  {
      if (!empty($_POST)) {
  
          // validate the ReCaptcha, if something is wrong, we throw an Exception, 
          // i.e. code stops executing and goes to catch() block
  
          if (!isset($_POST['g-recaptcha-response'])) {
              throw new \Exception('ReCaptcha is not set.');
          }
  
          // do not forget to enter your secret key in the config above 
          // from https://www.google.com/recaptcha/admin
  
          $recaptcha = new \ReCaptcha\ReCaptcha($recaptchaSecret, new \ReCaptcha\RequestMethod\CurlPost());
  
          // we validate the ReCaptcha field together with the user's IP address
  
          $response = $recaptcha-&gt;verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);
  
  
          if (!$response-&gt;isSuccess()) {
              throw new \Exception('ReCaptcha was not validated.');
          }
  
          // everything went well, we can compose the message, as usually
  
          $emailText = "You have new message from your website contact form\n==========================================\n\n";
  
          foreach ($_POST as $key =&gt; $value) {
  
              if (isset($fields[$key])) {
                  $emailText .= "$fields[$key]: $value\n\n";
              }
          }
  
  
          $headers = array('Content-Type: text/plain; charset="UTF-8";',
              'From: ' . $from,
              'Reply-To: ' . $from,
              'Return-Path: ' . $from
          );
  
          mail($sendTo, $subject, $emailText, implode("\n", $headers));
  
          $responseArray = array('type' =&gt; 'success', 'message' =&gt; $okMessage);
      }
  }
  catch (\Exception $e)
  {
      $responseArray = array('type' =&gt; 'danger', 'message' =&gt; $errorMessage);
  }
  
  if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) &amp;&amp; strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
      $encoded = json_encode($responseArray);
  
      header('Content-Type: application/json');
  
      echo $encoded;
  }
  else {
      echo $responseArray['message'];
  }
  
  ?&gt;
  </code></pre>