2016-09-17 57 views
0

注:我已閱讀其他問題,他們沒有幫助。PHPMailer與AJAX鏈接不起作用

所以我第一次測試在我的本地使用Ajax和PHPMailer的,當我收到的電子郵件,它的工作完美,並與阿賈克斯它說電子郵件發送。

當我上傳到活動網站,它停止工作。我確信連接是穩定的,因爲當我拿走AJAX並將它作爲一個PHP頁面使用時。但是,當它與AJAX外部文件時,它不會(在直播網站)

這裏是AJAX代碼:

<script> 
    var ajax = { 
     isSubmiting: false, 
     send: function() { 

      if(ajax.isSubmiting == false) { 
       ajax.isSubmiting = true; 

       var userName = $("input[name=name]").val(); 
       var userEmail = $("input[name=email]").val(); 
       var userComments = $("textarea").val(); 
       var currentBusiness = $("input[name=business").val(); 
       var currentWebsite = $("input[name=website]").val(); 

        ajax.SetText("Sending..."); 
        $.post("sendmail.php", { 
         name: userName, email: userEmail, comments: userComments, business: currentBusiness, website: currentWebsite 
        }, function(data) { 
         if(data == "true") { 

          ajax.SetText("Sent!"); 

          $.get("sent.html", function(sentData){ 

           $("#content").html(sentData); 

          }); 

         } else { 
          ajax.SetText("Send mail"); 

          $.get("unsent.html", function(sentData){ 

           $("#content").html(sentData); 

          }); 

          console.log(); 
         } 

         ajax.isSubmiting = false; 
        }); 

      } 
      else alert("You can only send 1 email at a time"); 
     }, 
     SetText: function(text) { 
      $("input[type=button]").val(text); 
     } 
    } 
</script> 

這裏是PHPMailer的腳本(sendmail.php):

<?php 

    $result = ""; 
    $error = ""; 

    if(count($_POST) > 0) { 

      $message= 
      'Full Name: '.$_POST['name'].'<br /> 
      Email: '.$_POST['email'].'<br /> 
      Message: '.$_POST['comments'].'<br /> 
      Current Website: '.$_POST['website'].'<br /> 
      Business Name: '.$_POST['business'].'<br /> 
      '; 

      include "phpmailer/class.smtp.php"; 
      include("includes/class.phpmailer.php"); 

      $mail = new PHPMailer(); 
      //Tell PHPMailer to use SMTP 
      $mail->isSMTP(); 
      //Enable SMTP debugging 
      // 0 = off (for production use) 
      // 1 = client messages 
      // 2 = client and server messages 
      $mail->Host = localhost; 
      //Set who the message is to be sent from 
      $mail->setFrom('[email protected]', 'Coherent'); 
      //Set an alternative reply-to address 
      //$mail->addReplyTo('[email protected]', 'First Last'); 
      //Set who the message is to be sent to 
      $mail->addAddress('[email protected]', 'Coherent'); 
      //Set the subject line 
      $mail->Subject = 'Contact Form'; 
      //Read an HTML message body from an external file, convert referenced images to embedded, 
      //convert HTML into a basic plain-text alternative body 
      $mail->MsgHTML($message); 
      //Replace the plain text body with one created manually 
      $mail->AltBody = 'This is a plain-text message body'; 

      if (!$mail->send()) { 
       die("There was an error sending the email."); 
      } else { 
       die("true");  
      } 


     } 

?> 

沒有任何人有一個線索,爲什麼這會不會說。僅僅重申一下,在一個沒有Ajax的PHP頁面上,並且在腳本中,它可以在實時和本地服務器上正常工作。但是,當我將它放入外部文件並與AJAX鏈接時,它只能在本地服務器上運行,而不能在現場運行。任何答案或建議將不勝感激。

+0

定義「不起作用」。幾乎任何ajax系統都傾向於需要JSON格式的返回值,你不這樣做。您也沒有使用PHPMailer的錯誤報告功能,這並不意外,這意味着您無法確定發生了什麼。 – Synchro

+0

@Synchro我確實啓用了錯誤報告,但我無法準確查看它們,而不是在控制檯中,而不是在頁面上,或任何東西。那是因爲它的外部文件?而不是工作,我的意思是說它沒有發送和不發送。 –

+0

如果您看到它說它未能發送,您可以看到錯誤輸出。嘗試簡單地說'死(「發送電子郵件時發生錯誤。」。$ mail-> ErrorInfo);'開始。 – Synchro

回答

0

你已經在你的腳本中使用相對路徑。從Ajax調用時,請確保路徑正在轉換爲正確的路徑。

您可以檢查控制檯檢查Ajax調用正常工作,然後服務器錯誤日誌,以確認沒有PHP錯誤。

+0

這不提供問題的答案。要批評或要求作者澄清,請在其帖子下方留言。 - [來自評論](/ review/low-quality-posts/13707260) – Jonathan

+0

哦,好的,當然。只是提供建議來獲取系統報告的錯誤。下次使用問題評論。 –