2014-07-06 27 views
0

我正在使用php郵件發送我的聯繫表單。儘管我沒有收到電子郵件,但我能夠獲得正在發送的電子郵件的成功消息。 這裏是我的代碼:即使在調試PhpMailer之後,我仍然無法收到電子郵件

contact.php

<form method="post" id="contactForm" action="contactProcess.php"> 
    <div class="clearfix"> 
     <div class="grid_6 alpha fll"><input type="text" name="senderName" id="senderName" placeholder="Name *" class="requiredField" /></div> 
     <div class="grid_6 omega flr"><input type="text" name="senderEmail" id="senderEmail" placeholder="Email Address *" class="requiredField email" /></div> 
    </div> 
    <div><textarea name="message" id="message" placeholder="Message *" class="requiredField" rows="8"></textarea></div> 
    <input type="submit" id="sendMessage" name="sendMessage" value="Send Email" /> 
    <span> </span> 
</form> 

contactProcess.php

<?php 
include 'library.php'; // include the library file 
include "classes/class.phpmailer.php"; // include the class name 
if(isset($_POST["sendMessage"])){ 
    $name = $_POST['senderName']; 
    $email = $_POST['senderEmail']; 
    $message = $_POST['message']; 

    $mail = new PHPMailer; // call the class 
    $mail->IsSMTP(); 
    $mail->SMTPDebug = 2; 
    $mail->Host = SMTP_HOST; //Hostname of the mail server 
    $mail->Port = SMTP_PORT; //Port of the SMTP like to be 25, 80, 465 or 587 
    $mail->SMTPAuth = true; //Whether to use SMTP authentication 
    $mail->Username = SMTP_UNAME; //Username for SMTP authentication any valid email created in your domain 
    $mail->Password = SMTP_PWORD; //Password for SMTP authentication 
    $mail->From = $email; //From address of the mail 
    $mail->FromName = $name; 
    $mail->Subject = ("Mail From Contact Form"); //Subject of your mail 

    $mail->IsHTML(true); 
    $mail->AddAddress("[email protected]");//To address who will receive this email 
    $mail->AddCC("[email protected]"); 
    $mail->AddReplyTo("[email protected]", "Me"); 
    $mail->Body = $message; 
    $mail->AltBody = $message; 

    $send = $mail->Send(); //Send the mails 
    if($send){ ?> 
     <script language="javascript" type="text/javascript"> 
     alert('sent'); 
     window.location = 'contact.php'; 
    </script> 
    <?php 
    } 
    else{ ?> 
     <script language="javascript" type="text/javascript"> 
     alert('Message failed'); 
     window.location = 'contact.php'; 
    </script> 
    <?php 
    } 
} 
?> 

終於AJAX驗證腳本: main.js

// Ajax Contact 
if ($("#contactForm")[0]) { 
    $('#contactForm').submit(function() { 
     $('#contactForm .error').remove(); 
     $('.requiredField').removeClass('fielderror'); 
     $('.requiredField').addClass('fieldtrue'); 
     $('#contactForm span strong').remove(); 
     var hasError = false; 
     $('#contactForm .requiredField').each(function() { 
      if (jQuery.trim($(this).val()) === '') { 
       var labelText = $(this).prev('label').text(); 
       $(this).addClass('fielderror'); 
       $('#contactForm span').html('<strong>*Please fill out all fields.</strong>'); 
       hasError = true; 
      } else if ($(this).hasClass('email')) { 
       var emailReg = /^([\w-\.][email protected]([\w-]+\.)+[\w-]{2,4})?$/; 
       if (!emailReg.test(jQuery.trim($(this).val()))) { 
        var labelText = $(this).prev('label').text(); 
        $(this).addClass('fielderror'); 
        $('#contactForm span').html('<strong>Your email address is incorrect</strong>'); 
        hasError = true; 
       } 
      } 
     }); 
     if (!hasError) { 
      $('#contactForm').slideDown('normal', function() { 
       $("#contactForm #sendMessage").addClass('load-color'); 
       $("#contactForm #sendMessage").attr("disabled", "disabled").val('Sending message. Please wait...'); 
      }); 
      var formInput = $(this).serialize(); 
      $.post($(this).attr('action'), formInput, function (data) { 
       $('#contactForm').slideUp("normal", function() { 
        $(this).before('<div class="notification-box notification-box-success"><p><i class="icon-ok"></i>Thanks!</strong> Your email was successfully sent. We will get back to you soonest!.</p></div>'); 
       }); 
      }); 
     } 
     return false; 
    }); 
} 

我有試圖設置$ mail-> SMTPDebug = 2;還包括error_reporting(E_ALL);希望我能得到任何錯誤,但沒有任何反應。

任何想法,請!

+0

您是否有權訪問Web服務器中的郵件日誌?您可能會遇到本地郵件程序配置問題,這些郵件可能會被本地接受,但不會離開您的websrv。如果您正在Linux/UNIX服務器上工作,並且您可以訪問它的shell控制檯,請嘗試使用「mail」命令發送一個命令,並查看它是否到達目的地。 –

+0

不幸的是,我沒有像在共享的Windows服務器上那樣訪問郵件日誌。有沒有辦法解決它? – Josealonso14

+0

我已經與我的託管解決方案聯繫,他們說他們已經通過網絡郵件測試了該帳戶的郵件服務,並且工作正常,所以它必須與腳本有關。 – Josealonso14

回答

0

我在我的服務器上試過你的代碼,相應地設置了SMTP_ *值。在我的情況下,主機是「localhost」,端口25,SMTP驗證「false」。消息已正確排隊。

所以我的猜測是郵件設置存在問題。檢查所有SMTP_ *值(我假設你是在library.php中設置的還是在它所包含的文件中)都是正確的。

評論window.location()調用時,我能夠從SMTP調試中看到此輸出。

SMTP -> FROM SERVER: 220 <server name edited> ESMTP Postfix (Debian/GNU) 
SMTP -> FROM SERVER: 250-<server name edited> 250-PIPELINING 250-SIZE 10240000 250-VRFY 250-ETRN 250-ENHANCEDSTATUSCODES 250-8BITMIME 250 DSN 
SMTP -> FROM SERVER: 250 2.1.0 Ok 
SMTP -> FROM SERVER: 250 2.1.5 Ok 
SMTP -> FROM SERVER: 354 End data with . 
SMTP -> FROM SERVER: 250 2.0.0 Ok: queued as C39A6440AA 
SMTP -> FROM SERVER: 221 2.0.0 Bye 

總之,你的代碼似乎沒問題,你可能有一個配置問題。

希望這會有所幫助!

+0

@ Mariano:是的,我正在設置library.php中的smtp值,我想我相信我正在使用正確的值,因爲我已經完全檢查了它們。除非,在我的情況下,SMTP_USNAME是我設置的另一個郵件賬戶不是正確的使用價值(我已將它設置爲:[email protected],並使用電子郵件賬戶的密碼作爲SMTP_PASSWORD)。你是否認爲這是問題?如果不是這樣,我現在唯一的擔心就是你說過你使用過SMTP驗證「false」..我應該從SMTP驗證更改爲「真」SMTP驗證「false」嗎?再次感謝你們的支持! – Josealonso14

+0

@ Josealonso14,這一切都取決於服務器設置。在我的測試中,auth沒有必要,但生產服務器通常不是這樣。即使如此,由於您的代碼只是通過更改配置而工作,我建議您嘗試在另一臺服務器上運行它(可能在您的計算機上設置本地LAMP/WAMP?),或者使用其他郵件提供程序,或者兩者兼而有之。 –

相關問題