2011-01-25 134 views
5

如何禁用來自PHPMailer類的錯誤消息?我正在顯示自己的錯誤消息,並且不希望用戶看到諸如「SMTP錯誤:無法連接到SMTP主機」等錯誤。禁用PHPMailer錯誤消息

我的代碼:http://pastie.org/1497819

感謝

+0

PHPMailer的有一個`verbose`屬性。我沒有一個方便的副本,所以我不能檢查確切的名稱,但它是這樣的效果 - 檢查類文件的頂部,它應該在那裏 – 2011-01-25 20:35:30

+0

我找到$ ErrorInfo但它並不重要如果我註釋掉該行。仍顯示錯誤消息。 – Richard 2011-01-25 20:49:09

回答

3

這是不是可能是最好的解決方案,但它的工作原理。

在你PHPMailer的庫文件夾打開 「class.phpmailer.php」,

找到

public function Send() 

裏面註釋行

echo $e->getMessage()."\n"; 
+1

$ mail-> SMTPDebug = 0;將停止所有警告/通知 – Sunit 2017-04-14 09:12:03

0

如果錯誤是從呼叫$mail->Send();到來,那麼你可以做這樣的事情:

$oldErrorSetting = error_reporting(0); // temporarily disable warnings 
$delivery = $mail->Send(); 
error_reporting($oldErrorSetting); // restore error setting 

這將抑制來自PHP錯誤發送呼叫。

然後,獲得的PHPMailer的錯誤,並做一些與他們很好,你可以使用$delivery返回值和ErrorInfo

if (!$delivery) { 
    $errorDetails = $mail->ErrorInfo; 
    print "<p>$errorDetails</p>\n"; 
} 
+0

我不能讓它工作,我必須做錯了什麼,我試着實現你的代碼,但沒有運氣。請參閱我的原始代碼http://pastie.org/1497819。 – Richard 2011-01-26 00:01:49

23

我知道這個線程是老已經回答,但我在這裏偶然發現,因爲我有同樣的問題,但最終以不同的方式解決它,所以我想我會分享。注意:我正在使用PHPMailer v5.1。

當你實例化PHPMailer類時,它需要一個可選參數$ exceptions。這告訴PHPMailer它是否應該在遇到任何異常時拋出異常。它默認爲false,這意味着它不會拋出任何異常,只是迴應它的消息。但是,如果您將它稱爲

$mail = new PHPMailer(true); 

您會告訴它拋出異常。然後,您可以捕捉這些例外情況,然後根據您的選擇進行處理。對我來說,這比干擾源代碼或禁用錯誤報告更清潔和更優雅。

+0

這是IMO的最佳答案,謝謝希思。 – 2012-11-17 01:06:11

+0

這是最好的答案。同意。 – CaptSaltyJack 2014-11-18 23:26:41

2

有一個更好的解決方案(我認爲:)),它是由裏卡多

sugested在文件「class.phpmailer.php」解決方案的改進版本,在函數內部

public function Send() 

找到行

echo $e->getMessage()."\n"; 

並更換該

if ($SMTPDebug) 
    echo $e->getMessage()."\n"; 

這樣,它只會顯示異常消息(或錯誤消息),如果您正在調試模式下運行..

希望它有幫助! 祝福!

2

這裏的另一種解決方案:

ob_start(); //start output buffering to capture all output and prevent errors from being displayed 
$delivery = $mail->Send(); 
ob_end_clean(); //erase the buffer and stop output buffering 

我需要從JUtility ::在Sendmail中的Joomla功能,它使用PHPMailer的幕後抑制誤差,所以上述的答案並沒有爲我,但輸出緩衝工作訣竅。

9

這是PHPMailer希望你這樣做的方式;不涉及編輯原始類文件。

$mail->SMTPDebug = false; 
$mail->do_debug = 0; 
2
//Enable SMTP debugging 
// 0 = off (for production use) 
// 1 = client messages 
// 2 = client and server messages 
$mail->SMTPDebug = 2; 
0

我在與該錯誤的問題:無法修改標題信息 - 頭已經發出(輸出開始/phpMailer/class.phpmailer.php),因爲我返回JSON需要頭( 'Content-Type:application/json');並且因爲class.phpmailer.php類在屏幕上顯示錯誤Invalid address:這會產生錯誤,因此解決這個問題非常簡單,它只包含在類class.phpmailer.php中的以下行中調試檢查和它的工作完全追隨

此行

Echo $ this-> Lang ('invalid_address'). ':'. $ Address; 

更改爲:

If ($ this-> SMTPDebug) { 
       Echo $ this-> Lang ('invalid_address'). ':'. $ Address; 
  }