I am trying to test this sendEmail.php script with XAMPP but it is not working. I believe the script is good but I am not sure if I am testing it in correct way. I changed the e-mail address in the php script (below) to my own e-mail, filled the contact form and pressed 'send' button but the message never reaches my inbox. The loader is spinning all the time but nothing happens.
I also tried to change blank action='' into action='inc/sendEmail.php' in HTML code but nothing happens. I started XAMPP and copy/pasted all the documents in htdocs folder. The page loads normally when I access it through localhost but besides the loader spinning all the time, I never receive any confirmation that my message is sent (or not) and it never reaches my inbox. There is a small ajax snippet at the bottom. Can that be the problem? Can Ajax be tested with XAMPP or maybe this is not the way to test this form via XAMPP? I would appreciate if someone could help me to make this work. Thank you for your time!
As my PHP code is significantly different than other codes I found on Stackoverflow I still cannot figure out the answer? Could anyone please point me in the right direction? I would really like to use this code not some other. Thanks.
Here is the sendEmail.php:
<?php
// Replace this with your own email address
$siteOwnersEmail = 'user@website.com';
if($_POST) {
$fname = trim(stripslashes($_POST['contactFname']));
$lname = trim(stripslashes($_POST['contactLname']));
$email = trim(stripslashes($_POST['contactEmail']));
$subject = trim(stripslashes($_POST['contactSubject']));
$contact_message = trim(stripslashes($_POST['contactMessage']));
// Check First Name
if (strlen($fname) < 2) {
        $error['fname'] = "Please enter your first name.";
}
// Check Last Name
if (strlen($lname) < 2) {
    $error['lname'] = "Please enter your last name.";
}
// Check Email
if (!preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
    $error['email'] = "Please enter a valid email address.";
}
// Check Message
if (strlen($contact_message) < 15) {
    $error['message'] = "Please enter your message. It should have at least 15 characters.";
}
// Subject
if ($subject == '') { $subject = "Contact Form Submission"; }
// Set Name
$name = $fname . " " . $lname;
// Set Message
$message .= "Email from: " . $name . "<br />";
$message .= "Email address: " . $email . "<br />";
$message .= "Message: <br />";
$message .= $contact_message;
$message .= "<br /> ----- <br /> This email was sent from your site's contact form. <br />";
// Set From: header
$from =  $name . " <" . $email . ">";
// Email Headers
$headers = "From: " . $from . "\r\n";
$headers .= "Reply-To: ". $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if (!$error) {
  ini_set("sendmail_from", $siteOwnersEmail); // for windows server
  $mail = mail($siteOwnersEmail, $subject, $message, $headers);
    if ($mail) { echo "OK"; }
  else { echo "Something went wrong. Please try again."; }
} # end if - no validation error
else {
    $response = (isset($error['fname'])) ? $error['fname'] . "<br /> \n" : null;
    $response .= (isset($error['lname'])) ? $error['lname'] . "<br /> \n" : null;
    $response .= (isset($error['email'])) ? $error['email'] . "<br /> \n" : null;
    $response .= (isset($error['message'])) ? $error['message'] . "<br />" : null;
    echo $response;
} # end if - there was a validation error
}
?>
Here is the HTML:
<section id="contact">
    <h1 align="center">GET IN TOUCH WITH US<span>.</span></h1>
    <hr />
    <div class="row form-section">
        <div id="contact-form" class="twelve columns">
            <form name="contactForm" id="contactForm" method="post" action="">
                <fieldset>
                    <div class="row">
                        <div class="six columns mob-whole">
                            <label for="contactFname">First Name <span class="required">*</span></label>                                   
                                <input name="contactFname" type="text" id="contactFname" placeholder="First Name" value="" />                           
                        </div>
                        <div class="six columns mob-whole"> 
                            <label for="contactLname">Last Name <span class="required">*</span></label>                                
                                <input name="contactLname" type="text" id="contactLname" placeholder="Last Name" value="" />                            
                        </div>                          
                    </div>
                    <div class="row">
                        <div class="six columns mob-whole"> 
                            <label for="contactEmail">Email <span class="required">*</span></label>                                
                                <input name="contactEmail" type="text" id="contactEmail" placeholder="Email" value="" />                            
                        </div>
                        <div class="six columns mob-whole">  
                            <label for="contactSubject">Subject</label>                                
                                <input name="contactSubject" type="text" id="contactSubject" placeholder="Subject"  value="" />                         
                        </div>
                    </div>
                    <div class="row">
                        <div class="twelve columns">
                            <label  for="contactMessage">Message <span class="required">*</span></label>
                            <textarea name="contactMessage"  id="contactMessage" placeholder="Your Message" rows="10" cols="50" ></textarea>
                        </div>
                    </div>
                    <div>
                        <button class="submit full-width">Send Message</button>
                        <div id="image-loader">
                            <img src="images/loader.gif" alt="" />
                        </div>
                    </div>
                </fieldset>
            </form> <!-- /contactForm -->
            <!-- message box -->
            <div id="message-warning"></div>
            <div id="message-success">
               <i class="fa fa-check"></i>Your message was sent, thank you!<br />
            </div>
        </div> <!-- /contact-form -->       
    </div> <!-- /form-section -->     
</section>  <!-- /contact-->
Here is JavaScript/Ajax code:
$('form#contactForm button.submit').on('click', function() {
  $('#image-loader').fadeIn();
  var contactFname = $('#contactForm #contactFname').val();
  var contactLname = $('#contactForm #contactLname').val();
  var contactEmail = $('#contactForm #contactEmail').val();
  var contactSubject = $('#contactForm #contactSubject').val();
  var contactMessage = $('#contactForm #contactMessage').val();
  var data = 'contactFname=' + contactFname  + '&contactLname=' + contactLname + 
             '&contactEmail=' + contactEmail + '&contactSubject=' + contactSubject + 
             '&contactMessage=' + contactMessage;
  $.ajax({
      type: "POST",
      url: "inc/sendEmail.php",
      data: data,
      success: function(msg) {
        // Message was sent
        if (msg == 'OK') {
           $('#image-loader').fadeOut();
           $('#message-warning').hide();
           $('#contactForm').fadeOut();
           $('#message-success').fadeIn();   
        }
        // There was an error
        else {
           $('#image-loader').fadeOut();
           $('#message-warning').html(msg);
            $('#message-warning').fadeIn();
        }
      }
  });
  return false;
});
