Php form is saying that email has been sent but nothing has been recieved

HTML CODE

<div id="booking">
  <div class="container">
    <div class="section-title text-center">
      <h3>Please provide us with the following information</h3>
    </div>
    <div class="col-md-8 col-md-offset-2">
      <form name="sentMessage" id="contactForm" novalidate>
        <div class="row">

            <div class="col-md-6">
            <div class="form-group">
               Please select preferred survey day/time from the following: <br>
  <input type="radio" name="time" value="Mon-morning-9am-12pm">  Monday morning 9am-12pm
                <br>
  <input type="radio" name="time" value="Mon-afternoon-1pm-4pm">  Monday afternoon 1pm-4pm
                <br> 
  <input type="radio" name="time" value="Wed-morning-9am-12pm"> Wednesday morning 9am-12pm
                <br>
  <input type="radio" name="time" value="Wed-afternoon-1pm-4pm">  Wednesday afternoon 1pm-4pm
              <p class="help-block text-danger"></p>
            </div>
          </div>
            <br>
          <div class="col-md-6">
            <div class="form-group">
              <input type="text" id="name" class="form-control" placeholder="Name" required="required">
              <p class="help-block text-danger"></p>
            </div>
          </div>
          <div class="col-md-6">
            <div class="form-group">
              <input type="email" id="email" class="form-control" placeholder="Email" required="required">
              <p class="help-block text-danger"></p>
            </div>
          </div>
        </div> 
              <div class="row">
          <div class="col-md-6">
            <div class="form-group">
              <input type="text" id="number" class="form-control" placeholder="Number to best reach you on: mobile, work" required="required">
              <p class="help-block text-danger"></p>
            </div>
          </div>

                  <div class="col-md-6">
            <div class="form-group">
              <input type="text" id="date" class="form-control" placeholder="Potential Moving Date" required="required">
              <p class="help-block text-danger"></p>
            </div>
        </div> 
        
        <div id="success"></div>
        <button type="submit" class="btn btn-custom btn-lg">Send Message</button>
      </form>
    </div>
  </div>
</div>

PHP Code

<?php
// Check for empty fields
if (empty($_POST['time']) 		||
   empty($_POST['name'])  		||
   empty($_POST['email']) 		||
   empty($_POST['number'])  		||
   empty($_POST['date'])	||
   !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
   {
	echo "No arguments Provided!";
	return false;
   }
	
$time = $_POST['time'];
$name = $_POST['name']; 
$email_address = $_POST['email'];
$number = $_POST['number'];
$date = $_POST['date'];
	
// Create the email and send the message
$to = 'me@example.co.uk'; // Add your email address inbetween the '' replacing yourname@yourdomain.com - This is where the form will send a message to.
$email_subject = "Website Contact Form:  $name";
$email_body = "You have received a new message from your website contact form: Arrange a Survey.\n\n"."Here are the details:\n\nTime: $time \n\nName: $name \n\nEmail: $email_address \n\nNumber: $number \n\nDate:\n$date";
$headers = "From: noreply@yourdomain.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
$headers .= "Reply-To: $email_address";	
mail($to,$email_subject,$email_body,$headers);
return true;			
?>

Javascript Code

$(function() {

    $("input,textarea").jqBootstrapValidation({
        preventSubmit: true,
        submitError: function($form, event, errors) {
            // additional error messages or events
        },
        submitSuccess: function($form, event) {
            event.preventDefault(); // prevent default submit behaviour
            // get values from FORM
            var time = $("input#time").val();
            var name = $("input#name").val();
            var email = $("input#email").val();
            var number = $("input#number").val();
            var date = $("input#date").val();
            var firstName = name; // For Success/Failure Message
            // Check for white space in name for Success/Fail message
            if (firstName.indexOf(' ') >= 0) {
                firstName = name.split(' ').slice(0, -1).join(' ');
            }
            $.ajax({
                url: "././mail/survey.php",
                type: "POST",
                data: {
                    time: time,
                    name: name,
                    email: email,
                    number: number,
                    date: date
                },
                cache: false,
                success: function() {
                    // Success message
                    $('#success').html("<div class='alert alert-success'>");
                    $('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
                        .append("</button>");
                    $('#success > .alert-success')
                        .append("<strong>Your message has been sent. </strong>");
                    $('#success > .alert-success')
                        .append('</div>');

                    //clear all fields
                    $('#contactForm').trigger("reset");
                },
                error: function() {
                    // Fail message
                    $('#success').html("<div class='alert alert-danger'>");
                    $('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
                        .append("</button>");
                    $('#success > .alert-danger').append("<strong>Sorry " + firstName + ", it seems that my mail server is not responding. Please try again later!");
                    $('#success > .alert-danger').append('</div>');
                    //clear all fields
                    $('#contactForm').trigger("reset");
                },
            })
        },
        filter: function() {
            return $(this).is(":visible");
        },
    });

    $("a[data-toggle=\"tab\"]").click(function(e) {
        e.preventDefault();
        $(this).tab("show");
    });
});


/*When clicking on Full hide fail/success boxes */
$('#name').focus(function() {
    $('#success').html('');
}); 

Hi @seamycfc1. Welcome to the SP forum. Did you try a test email? Without all the form variables?

Edit: I just noticed that, except for the time, you don’t use a name attribute for any of the form fields?

Hi there, thank you so much for getting back to me!
Yeah I tried a test email it still wont come through would you have any ideas?

Would that be the case of me changing the ‘id’ tags to ‘name’ instead?

I mean a test email without the form?

sorry I don’t know exactly what you mean, I’m still getting use to PHP.

Just make a test page call it whatever you want. At the following to the page:

<?php
$to = // your email
$subject = // test subject
$body  = // test text
$headers = // Headers

mail($to,$subject,$body,$headers);

Save page, upload the page to the server, navigate to page and see if its working

It didnt work when I tried to navigate to it url is below:

http://notoriousdesigns.co.uk/mail/test.php

Did you receive the email?

do I navigate to that page by submitting the form?

No just use the url in the addressbar

When you send the email using the mail() function, you don’t check the return from that function, you always return true; at the end of your code, hence the calling Ajax code says it has worked whether it did or not.

In any case, I think you need to look at setting the HTTP status code rather than using return to indicate whether the mail worked, once you add the code to check. I’m not that experienced with JS, but normally return is used to send a value back from a function to the calling code, within the same execution. As your JS code is calling a server and starting an entirely isolated script running on it, I don’t think return will work here.

Many people in here recommend that you do not use the PHP mail() function for sending mail, and instead use a library like PHPMailer or another one that I forget the name of. Either way, though, you need to get your configuration set up to know where to send mails, what account to use and so on.

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.