1

I have setup a form to send out an email upon clicking sending. To test it, I have hard coded the values in the script. I even get a success message "Yes" when I click send but unable to receive the email. Checked that the email address is correct and checked spam folder too. Please advice if I am missing something. Thanks.

HTML

<form action="mail/contact_me.php" method="post" role="form" id="formid" class="footer-custom-form footer-custom-hr">
    <div class="row">
        <div class="col-md-12">
            <div class="form-group">
                <input type="text" class="form-control" name="name" placeholder="Your Name..." required>
            </div>
        </div>
    </div>
   <div class="row">
        <div class="col-md-6">
            <div class="form-group">
                <input type="text" class="form-control" name="phone" placeholder="Phone">
            </div>
        </div>
        <div class="col-md-6">
            <div class="form-group">
                <input type="email" class="form-control" name="email" placeholder="Your Email Address" required>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-md-12">
            <div class="form-group">
                <textarea class="form-control" name="message" rows="8" placeholder="Your Message" required></textarea>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="">
            <input class="footer-form-btn" type="submit" value="Send message">
        </div>
    </div>
</form>

JS

$(function () {
    $('form#formid').on('submit', function (e) {

      e.preventDefault();

      $.ajax({
        type: 'post',
        url: '../mail/contact_me.php',
        data: $('form#formid').serialize(),
        success: function () {
            alert("yes"); //<-- I get this alert after submit
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert("no");
        }
      });
    });
});

PHP

<?php
//hardcoded for testing
$name = 'testname';
$email_address = 'testemail@gmail.com';
$subject = 'testsubject';
$message = 'testmessage';

// Create the email and send the message
$to = 'myemail@gmail.com'; // I have my actual email here
$email_subject = "Website Contact Form:  $name";
$email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nSubject: $subject\n\nMessage:\n$message";
$headers = "From: noreply@test.com\n"; 
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;
?>
kar
  • 4,791
  • 12
  • 49
  • 74
  • As a test, modify your `mail($to,$email_subject,$email_body,$headers); return true;` to `if(mail($to,$email_subject,$email_body,$headers)){ echo "Sent"; } else { echo "Failed"; }`, what happens? – Funk Forty Niner Jul 23 '17 at 14:37
  • i think you need to configure SMTP details in PHP.ini or install a local SMTP server to send emails – Raymond Nijland Jul 23 '17 at 14:38
  • @Fred-ii- Tested and I get the echo 'Sent'. – kar Jul 23 '17 at 14:42
  • mail did its job then. Check your server's config and use error reporting to see if it yields anything. – Funk Forty Niner Jul 23 '17 at 14:42
  • @RaymondNijland I am testing directly on a hosted server. All files been uploaded correctly. – kar Jul 23 '17 at 14:43
  • also try it without the JS using pure PHP. If that works, then your JS failed. – Funk Forty Niner Jul 23 '17 at 14:45
  • @Fred-ii- I tried the test you mentioned without the js thus still failing with or without js. – kar Jul 23 '17 at 14:46
  • see if there's anything from error reporting http://php.net/manual/en/function.error-reporting.php - maybe your host is throwing you a false positive and mail isn't enabled on it. There isn't anything else I can add here, sorry. – Funk Forty Niner Jul 23 '17 at 15:05
  • @Fred-ii- It was an issue with the server. Resolved now. Thanks. – kar Jul 23 '17 at 16:47
  • 1
    @kar ah.... I had a *spidey sense tingle* about that. Glad to hear that it was resolved, *cheers* and you're welcome. – Funk Forty Niner Jul 23 '17 at 16:48

0 Answers0