2013-01-15 54 views
0

我有一個AJAX形式這是說它的成功,但仍然沒有收到任何電子郵件,這很煩人。阿賈克斯形式發送成功,但沒有電子郵件

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> 

<script type="text/javascript"> 

/* 
    Code by David McKeown 
    www.craftedbydavid.com 
*/ 

jQuery(document).ready(function() { // wait until the document is ready 
    jQuery('#send').click(function(){ // when the button is clicked the code executes 
     jQuery('.error').fadeOut('slow'); // reset the error messages (hides them) 

     var error = false; // we will set this true if the form isn't valid 

     var name = jQuery('input#name').val(); // get the value of the input field 
     if(name == "" || name == " ") { 
      jQuery('#err-name').fadeIn('slow'); // show the error message 
      error = true; // change the error state to true 
     } 

     var phone = jQuery('input#phone').val(); // get the value of the input field 
     if(phone == "" || phone == " ") { 
      jQuery('#err-phone').fadeIn('slow'); // show the error message 
      error = true; // change the error state to true 
     } 

     var email_compare = /^([a-z0-9_.-]+)@([da-z.-]+).([a-z.]{2,6})$/; // Syntax to compare against input 
     var email = jQuery('input#email').val(); // get the value of the input field 
     if (email == "" || email == " ") { // check if the field is empty 
      jQuery('#err-email').fadeIn('slow'); // error - empty 
      error = true; 

     }else if (!email_compare.test(email)) { // if it's not empty check the format against our email_compare variable 
      jQuery('#err-emailvld').fadeIn('slow'); // error - not right format 
      error = true; 
     } 

     if(error == true) { 
      jQuery('#err-form').slideDown('slow'); 
      return false; 
     } 

     var data_string = jQuery('#ajax-form').serialize(); // Collect data from form 
     //alert(data_string); 

     jQuery.ajax({ 
      type: "POST", 
      url: jQuery('#ajax-form').attr('action'), 
      data: data_string, 
      timeout: 6000, 
      error: function(request,error) { 
       if (error == "timeout") { 
        jQuery('#err-timedout').slideDown('slow'); 
       } 
       else { 
        jQuery('#err-state').slideDown('slow'); 
        jQuery("#err-state").html('An error occurred: ' + error + ''); 
       } 
      }, 
      success: function() { 
       jQuery('#ajax-form').slideUp('slow'); 
       jQuery('#ajaxsuccess').slideDown('slow'); 
      } 
     }); 

     return false; // stops user browser being directed to the php file 
    }); // end click function 
}); 

</script> 

HTML表單>>>>>>>>>>>>>>

<!-- language: lang-html --> 

<form name="ajax-form" id="ajax-form" action="mail-it.php" method="post"> 
    <p> 
     <label for="name">Name: * 
     <span class="error" id="err-name">please enter name</span> 
     <br /> 
     </label> 

     <input name="name" id="name" type="text" /><br /><br /> 

      <label for="phone">Contact Number: * 
     <span class="error" id="err-phone">please enter phone number</span> 
     <br /> 
     </label> 

     <input name="phone" id="phone" type="text" /> 

     <label for="email"> 
     <br /> 
     E-Mail: * 
     <span class="error" id="err-email">please enter e-mail</span> 
     <span class="error" id="err-emailvld">e-mail is not a valid format</span><br /> 
     </label> 
     <input name="email" id="email" type="text" /> 
     </p> 

    <p> 


     <label for="message">Message:</label> 
     <textarea name="message" id="message"></textarea> 

    <div class="myButton"><INPUT type="submit" name="" value="" id="send"></div> 


<div class="mainerror"> 
<div class="error" id="err-form">There was a problem validating the form please check!</div> 
    <div class="error" id="err-timedout">The connection to the server timed out!</div> 
    <div class="error" id="err-state"></div></div> 
</form> 
<div id="ajaxsuccess"><h2>Successfully sent!!</h2></div> 

MAIL PHP發送郵件,IT.PHP

<!-- language: lang-php --> 

<?php 



/* Code by David McKeown - craftedbydavid.com */ 

/* Editable entries are bellow */ 



$send_to = "[email protected]"; 

$send_subject = "Website Enquiry "; 







/*Be careful when editing below this line */ 



$f_name = cleanupentries($_POST["name"]); 

$f_phone = cleanupentries($_POST["phone"]); 

$f_email = cleanupentries($_POST["email"]); 

$f_message = cleanupentries($_POST["message"]); 

$from_ip = $_SERVER['REMOTE_ADDR']; 

$from_browser = $_SERVER['HTTP_USER_AGENT']; 



function cleanupentries($entry) { 

    $entry = trim($entry); 

    $entry = stripslashes($entry); 

    $entry = htmlspecialchars($entry); 



    return $entry; 

} 



$message = "This email was submitted on " . date('m-d-Y') . 

"\n\nName: " . $f_name . 

"\n\nPhone: " . $f_phone . 

"\n\nE-Mail: " . $f_email . 

"\n\nMessage: \n" . $f_message . 

"\n\n\nTechnical Details:\n" . $from_ip . "\n" . $from_browser; 



$send_subject .= " - {$f_name}"; 



$headers = "From: " . $f_email . "\r\n" . 

    "Reply-To: " . $f_email . "\r\n" . 

    "X-Mailer: PHP/" . phpversion(); 



if (!$f_email) { 

    echo "no email"; 

    exit; 

}else if (!$f_name){ 

    echo "no name"; 

    exit; 

}else if (!$f_phone){ 

    echo "no phone"; 

    exit; 

}else{ 

    if (filter_var($f_email, FILTER_VALIDATE_EMAIL)) { 

     mail($send_to, $send_subject, $message, $headers); 

     echo "true"; 

    }else{ 

     echo "invalid email"; 

     exit; 

    } 

} 



?> 

形式不說成功,但仍然沒有收到電子郵件!有任何想法嗎?

+2

檢查你的SMTP設置。 –

+0

如果AJAX成功,那麼您的問題純粹是PHP相關 –

+0

從服務器實際查看響應可能是一個好主意,而不是僅僅依賴返回* something *的事實。 –

回答

0

由於您保持提交按鈕類型,所以它以默認方式執行。

因此,在提交事件中,只需取消表單提交併運行您的AJAX代碼。

$('#ajax-form').bind('submit',function(e) { 
    e.preventDefault(); //Will prevent the submit... 
    //Add your code here 
}); 
+0

它們已經阻止了使用'return false'單擊提交按鈕,所以'submit'事件不會被解僱。 –

0

檢查你的錯誤日誌,這會給你更多關於真正發生的事情的信息。

+0

Arvind Bhardwaj - 謝謝 - 正確,我剛剛發現我們的一位員工今天早上已經超過了羣發郵件的門檻,因此特定域名無法接收電子郵件,經過其他域名電子郵件測試並正常工作。 – oneumbre

0

測試您的mail-it.php獨立。註釋掉jQuery('#send').click(function(){ ... }部分。然後在mail-it.php中添加以下行結束打印最後一個錯誤。

print_r(error_get_last()); // php >= 5.2.0 

現在嘗試提交form.This應當在提交和打印錯誤陣列重定向到mail-it.php腳本如果any.Try糾正,如果它拋出一些信息。