0

Is there a way i can verify if my php script for sending form to emails worked? I tried running the php website in localhost and tested to submit a message but when I checked my email, nothing has been sent. How do I kno if my form has been received? Is it instantly received when you sent a message from php form via email? How do I know if the email script was wrong or it just didnt send? heres my html:

<div class="container">
    <div class="row">
        <div class="inquiry2 col-xs-12 col-sm-12 col-md-12 col-lg-12">
        <br></br>
        <br></br>
        <h1>Message Us</h1>
            <form action="send.php" id="frmBox" method="post" onsubmit="return formSubmit();">
                <div class="form-group">
                    <label for="InputName">Name*</label>
                    <input class="form-control" type="text" placeholder="Name" name="customer_name" id="CustomerName">
                </div>
                <div class="form-group">    
                    <label for="InputEmail">Email Address*</label>
                    <input class="form-control" type="text" placeholder="email" name="customer_email" id="CustomerEmail">
                </div>
                <div class="form-group">    
                    <label for="InputPhone">Phone*</label>
                    <input class="form-control" type="text" placeholder="phone" name="customer_phone" id="CustomerPhone">
                </div>
                <div class="form-group">    
                    <label for="InputMessage">Message*</label>
                    <textarea class="form-control" type="text" placeholder="" name="customer_message" id="CustomerMessage"></textarea>
                </div>
                <input class="btn btn-default" type="submit" value="submit">
            </form>
        </div>
    </div>
    </div>
<!--SCRIPT-->
    <script type="text/javascript" src="js/jquery-3.2.1.min.js"></script>
    <script type="text/javascript">
        function formSubmit(){
            $.ajax({
                type:'POST',
                url:'send.php',
                data:$('#frmBox').serialize(),
                success:function(response){
                    $('#success').html(response);
                }
            });
            var form=document.getElementById('frmBox').reset();
            return false;
        }
    </script>

heres the php send:

<?php 


$customer_name_error = $customer_email_error = $customer_phone_error  = "";
$customer_name = $customer_email = $customer_phone = $customer_message= $success = "";

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["customer_name"])) {
    $customer_name_error = "Name is required";
  } else {
    $customer_name = test_input($_POST["customer_name"]);

    if (!preg_match("/^[a-zA-Z ]*$/",$customer_name_)) {
      $customer_name_error = "Only letters and white space allowed"; 
    }
  }

  if (empty($_POST["customer_email"])) {
    $customer_email_error = "Email is required";
  } else {
    $customer_email = test_input($_POST["customer_email"]);

    if (!filter_var($customer_email, FILTER_VALIDATE_EMAIL)) {
      $customer_email_error = "Invalid email format"; 
    }
  }

  if (empty($_POST["customer_phone"])) {
    $customer_phone_error = "Phone is required";
  } else {
    $customer_phone = test_input($_POST["customer_phone"]);

    if (!preg_match("/^(\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3}[\s-]?\d{4}$/i",$customer_phone)) {
      $customer_phone_error = "Invalid phone number"; 
    }
  }

  if (empty($_POST["customer_message"])) {
    $customer_message = "";
  } else {
    $customer_message = test_input($_POST["customer_message"]);
  }

  if ($customer_name_error == '' and $customer_email_error == '' and $customer_phone_error == '' ){
      $customer_message_body = '';
      unset($_POST['submit']);
      foreach ($_POST as $key => $value){
          $customer_message_body .=  "$key: $value\n";
      }

      $to = 'gorilyawarfare@gmail.com';
      $subject = 'Messages';
      if (mail($to, $subject, $message)){
          $success = "Message sent, thank you for contacting us!";
          $customer_name = $customer_email = $customer_phone = $customer_message = '';
      }
  }

}

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
?>
  • 1
    Possible duplicate of [Test PHP's mail function from localhost](https://stackoverflow.com/questions/3175488/test-phps-mail-function-from-localhost) – hassan Oct 07 '17 at 11:45
  • You are using php's `Mail()` function to actually send the mail. That relies on a correct setup of your php environment. In many cases it is easier to use some smtp class and configure it. But if you want to use the `mail()` function, then check your http servers log file for any errors. – arkascha Oct 07 '17 at 11:46
  • You have to use else with if conditions to show the errors. Because when if conditions are false then else can work to show the error or mistake in the code. – Rajpal Singh Oct 07 '17 at 11:50

0 Answers0