2014-04-20 42 views
0

好的,我猜這是簡單的東西,或者我正在使用我不知道的東西。PHPMailer失敗

我會從我的代碼中刪除多餘的位,但刪除的所有內容都已經過測試。

最初的代碼是基於一個非常普遍的教程腳本,我到處都可以看到。它是越來越到行:

while ($row = $res->fetch_array()) { 

和PHP與通病:調用一個成員函數fetch_array()非對象上。

通常這會標記我的SQL失敗或返回不需要的東西,我會有一個小的修復,然後繼續我的生活。然而,看起來並非如此。我一直在反覆SQL,它正在返回我想要的。

我認爲這可能與調用else內部的while循環(但這應該不是問題)或郵件調用在while循環內有關。

原始代碼:

$email = $link->real_escape_string($_POST["web_forgot_email"]); 
$sendemail = 0; 

$sql = "SQL to return Name and Email (Tested extensively and works)"; 
$res = $link->query($sql); 

if ($res->num_rows == 0) { 
    $arr = array("web_forgot_success" => "web no account"); 
    echo json_encode($arr); 
} else {  
    while ($row = $res->fetch_array()) { 
     $sendemail = $row['CLIENT_EMAIL']; 
     $name = $row['NAME']; 

     require("class.phpmailer.php"); 
     $mail = new PHPMailer();  
     $mail->IsSMTP(); 
     $mail->Host = "localhost"; 
     $mail->SMTPAuth = true; 
     $mail->Username = "[email protected]"; 
     $mail->Password = "quizzically01";     
     $mail->From = "[email protected]"; 
     $mail->FromName = "quizzically"; 
     $mail->AddAddress($sendemail); 
     $mail->WordWrap = 50; 
     $mail->IsHTML(true); 
     $mail->Subject = "quizzically password reset confirmation"; 
     $mailer = "Email Message"; 
     $mail->Body = $mailer; 
     if(!$mail->Send()) { 
     $arr = array("web_forgot_success" => "web no send"); 
     echo json_encode($arr); 
     } else { 
     $arr = array("web_forgot_success" => "web forgot success"); 
     echo json_encode($arr); 
     } 
    } 
} 

所以我rejigged代碼,以便找到變量$ sendemail和$ name,然後再發送一封電子郵件,但每次(即使$ sendemail變量不是0),它跳過如果聲明並返回else。我已經測試了$ sendemail變量,通過在else變量中將它發回給自己,它絕對不是0.

好東西是PHP不會失敗,壞事是有些東西導致代碼跳過整個如果部分處理髮送電子郵件。

Rejigged代碼:

$email = $link->real_escape_string($_POST["web_forgot_email"]); 
$sendemail = 0; 

$sql = "SQL to return Name and Email (Tested extensively and works)"; 
$res = $link->query($sql); 

if ($res->num_rows == 0) { 
    $arr = array("web_forgot_success" => "web no account"); 
    echo json_encode($arr); 
} 

while ($row = $res->fetch_array()) { 
    $sendemail = $row['CLIENT_EMAIL']; 
    $name = $row['NAME']; 
} 

if ($sendemail != 0) { 
    require("class.phpmailer.php");  
    $mail = new PHPMailer();  
    $mail->IsSMTP(); 
    $mail->Host = "localhost"; 
    $mail->SMTPAuth = true; 
    $mail->Username = "[email protected]"; 
    $mail->Password = "quizzically01";    
    $mail->From = "[email protected]"; 
    $mail->FromName = "quizzically"; 
    $mail->AddAddress($sendemail); 
    $mail->WordWrap = 50; 
    $mail->IsHTML(true); 
    $mail->Subject = "quizzically password reset confirmation";  
    $mailer = "Email Message"; 
    $mail->Body = $mailer; 

    if(!$mail->Send()) { 
     $arr = array("web_forgot_success" => "web no send"); 
     echo json_encode($arr); 
    } else { 
     $arr = array("web_forgot_success" => "web forgot success"); 
     echo json_encode($arr); 
    } 
} else { 
    $arr = array("web_forgot_success" => "web forgot failed"); 
    echo json_encode($arr); 
} 

我希望我做的東西不提倡使用的我還沒有發現或諸如此類的東西,但任何幫助,將不勝感激。

回答

0

答案是我自己無法完全測試。我在發送郵件的郵件中遇到了一個問題。這一定是拋出了錯誤,然後PHP在while循環中失敗。

錯誤的HTML修復(這是一個未轉義的雙引號),一切都沒有錯誤發送罰款。

相關問題