javascript - Trying to send an email with js/php. Says success but no email sends -
this question has answer here:
i have stared @ thing long enough figured time post question this. have code taken samples when fill form out , click send says success no email ever gets sent mailbox.
javascript
$(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 // values form var name = $("input#name").val(); var email = $("input#email").val(); var phone = $("input#phone").val(); var message = $("textarea#message").val(); var firstname = name; // success/failure message // check white space in name success/fail message if (firstname.indexof(' ') >= 0) { firstname = name.split(' ').slice(0, -1).join(' '); } $.ajax({ url: "././mail/contact_me.php", type: "post", data: { name: name, phone: phone, email: email, message: message }, cache: false, success: function() { alert("test1"); // success message $('#success').html("<div class='alert alert-success'>"); $('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×") .append("</button>"); $('#success > .alert-success') .append("<strong>your message has been sent. </strong>"); $('#success > .alert-success') .append('</div>'); //clear fields $('#contactform').trigger("reset"); }, error: function() { alert("test2"); // fail message $('#success').html("<div class='alert alert-danger'>"); $('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×") .append("</button>"); $('#success > .alert-danger').append("<strong>sorry " + firstname + ", seems mail server not responding. please try again later!"); $('#success > .alert-danger').append('</div>'); //clear fields $('#contactform').trigger("reset"); }, }) }, filter: function() { return $(this).is(":visible"); }, }); $("a[data-toggle=\"tab\"]").click(function(e) { e.preventdefault(); $(this).tab("show"); }); });
php
<?php // check empty fields if(empty($_post['name']) || empty($_post['email']) || empty($_post['phone']) || empty($_post['message']) || !filter_var($_post['email'],filter_validate_email)) { echo "no arguments provided!"; return false; } $name = $_post['name']; $email_address = $_post['email']; $phone = $_post['phone']; $message = $_post['message']; // create email , send message $to = 'myemail@something.com'; $email_subject = "website contact form: $name"; $email_body = "you have received new message website contact form.\n\n"."here details:\n\nname: $name\n\nemail: $email_address\n\nphone: $phone\n\nmessage:\n$message"; $headers = "from: myemail@something.com\n"; // email address generated message from. recommend using noreply@yourdomain.com. $headers .= "reply-to: $email_address"; mail($to,$email_subject,$email_body,$headers); return true; ?>
html
<form name="sentmessage" id="contactform" novalidate> <div class="row"> <div class="col-md-6"> <div class="form-group"> <input type="text" class="form-control" placeholder="your name *" id="name" required data-validation-required-message="please enter name."> <p class="help-block text-danger"></p> </div> <div class="form-group"> <input type="email" class="form-control" placeholder="your email *" id="email" required data-validation-required-message="please enter email address."> <p class="help-block text-danger"></p> </div> <div class="form-group"> <input type="tel" class="form-control" placeholder="your phone *" id="phone" required data-validation-required-message="please enter phone number."> <p class="help-block text-danger"></p> </div> </div> <div class="col-md-6"> <div class="form-group"> <textarea class="form-control" placeholder="your message *" id="message" required data-validation-required-message="please enter message."></textarea> <p class="help-block text-danger"></p> </div> </div> <div class="clearfix"></div> <div class="col-lg-12 text-center"> <div id="success"></div> <button type="submit" class="btn btn-xl">send message</button> </div> </div> </form>
well first of all, success/error handling in javascript checking if ajax call successful, second - not checking if mail function succeeded , return true in case
it should be
if( mail($to, $subject, $message, $headers) ) return true; else return false;
after check returned value in ajax callback
success: function(response){ if(response === true){ /* stuff */ } }
i've uploaded code server , seems work fine, xampp on other hand - didnt send mail. think forgot configure mail part, it's been while since used mail function locally
Comments
Post a Comment