title: Unable to send mail in php on shared server
body: <p>I'm unable to send mail on my shared server using ajax and php, is there a problem with the php mail configuration on the server, i'm using hostgator
  i have tried in my local ubuntu machine also but there also the same error
  as well as how can i get more detail on this error 
  the error i'm getting is </p>
  
  <hr>
  
  <p><a href="https://i.stack.imgur.com/U1mnB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/U1mnB.png" alt="Error in mail config"></a></p>
  
  <p>my code</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  $(document).ready(function () {
          $("#submit_btn").click(function () {
              var proceed = true;
              $("#contact_form input[required=true], #contact_form textarea[required=true]").each(function () {
                  $(this).css('border-color', '');
                  if (!$.trim($(this).val())) { //if this field is empty
                      $(this).css('border-color', 'red'); //change border color to red
                      proceed = false; //set do not proceed flag
                  }
                  var email_reg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
                  if ($(this).attr("type") == "email" &amp;&amp; !email_reg.test($.trim($(this).val()))) {
                      $(this).css('border-color', 'red'); //change border color to red
                      proceed = false; //set do not proceed flag
                  }
              });
              if (proceed) //everything looks good! proceed...
              {
                  post_data = {
                      'user_name': $('input[name=name]').val(),
                      'user_email': $('input[name=email]').val(),
                      'msg': $('textarea[name=message]').val()
                  };
                  $.post('contact/contact.php', post_data, function (response) {
                      if (response.type == 'error') { //load json data from server and output message
                          output = '&lt;div class="error"&gt;' + response.text + '&lt;/div&gt;';
                      } else {
                          output = '&lt;div class="success"&gt;' + response.text + '&lt;/div&gt;';
                          //reset values in all input fields
                          $("#contact_form input[required=true], #contact_form textarea[required=true]").val("");
                          $("#contact_form #contact_body").slideUp(); //hide form after success
                          window.setTimeout(function () { location.reload() }, 3000);
                      }
                      $("#contact_form #contact_results").hide().html(output).slideDown();
                  }, 'json');
              }
          });
          $("#contact_form input[required=true], #contact_form textarea[required=true]").keyup(function () {
              $(this).css('border-color', '');
              $("#contact_results").slideUp();
          });
      });
  &lt;/script&gt;
  </code></pre>
  
  <p>and my php code</p>
  
  <pre><code>&lt;?php
  if($_POST)
  {$to_email = "info@example.com"; //Recipient email, Replace with own email here
  
  //check if its an ajax request, exit if not
  if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
  
      $output = json_encode(array( //create JSON data
          'type'=&gt;'error', 
          'text' =&gt; 'Sorry Request must be Ajax POST'
      ));
      die($output); //exit script outputting json data
  }
  
  //Sanitize input data using PHP filter_var().
  $user_name      = filter_var($_POST["user_name"], FILTER_SANITIZE_STRING);
  $user_email     = filter_var($_POST["user_email"], FILTER_SANITIZE_EMAIL);
  $message        = filter_var($_POST["msg"], FILTER_SANITIZE_STRING);
  
  //additional php validation
  if(strlen($user_name)&lt;4){ // If length is less than 4 it will output JSON error.
      $output = json_encode(array('type'=&gt;'error', 'text' =&gt; 'Name is too short or empty!'));
      die($output);
  }
  if(!filter_var($user_email, FILTER_VALIDATE_EMAIL)){ //email validation
      $output = json_encode(array('type'=&gt;'error', 'text' =&gt; 'Please enter a valid email!'));
      die($output);
  }
  if(strlen($message)&lt;3){ //check emtpy message
      $output = json_encode(array('type'=&gt;'error', 'text' =&gt; 'Too short message! Please enter something.'));
      die($output);
  }
  
  //email body
  $message_body = $message."\r\n\r\n-".$user_name."\r\nEmail : ".$user_email."\r\nEmail : ". $user_email;
  $subject = "Mail From website";
  
  //proceed with PHP email.
  $headers = 'From: '.$user_name.'' . "\r\n" .
  'Reply-To: '.$user_email.'' . "\r\n" .
  'X-Mailer: PHP/' . phpversion();
  
  $send_mail = mail($to_email, $subject, $message_body, $headers);
  
  if(!$send_mail)
  {
      //If mail couldn't be sent output error. Check your PHP email configuration (if it ever happens)
      $output = json_encode(array('type'=&gt;'error', 'text' =&gt; 'Could not send mail! Please check your PHP mail configuration.'));
      die($output);
  }else{
      $output = json_encode(array('type'=&gt;'message', 'text' =&gt; 'Hi '.$user_name .' Thank you for your email'));
      die($output);
  }
  }
  ?&gt;
  </code></pre>