php 如何在 PHPMailer 中禁用错误消息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4798498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to disable error messages in PHPMailer?
提问by Richard
How do I disable the error messages from the PHPMailer class? I'm displaying my own error messages and I don't want users to see errors such as "SMTP Error: Could not connect to SMTP host."
如何禁用来自 PHPMailer 类的错误消息?我正在显示我自己的错误消息,但我不希望用户看到诸如“SMTP 错误:无法连接到 SMTP 主机”之类的错误。
Thanks
谢谢
采纳答案by Riccardo
This is not probably the best solution, but it works.
这可能不是最好的解决方案,但它有效。
In your phpmailer library folder open "class.phpmailer.php",
在你的 phpmailer 库文件夹中打开“class.phpmailer.php”,
find
找
public function Send()
inside it comment the line
在它里面评论这行
echo $e->getMessage()."\n";
回答by heath
I know this thread is old and already answered but I stumbled here because I had the same problem but ending up solving it differently so I thought I'd share. NOTE: I'm using PHPMailer v5.1.
我知道这个线程很旧并且已经得到了回答,但我在这里偶然发现,因为我遇到了同样的问题,但最终以不同的方式解决它,所以我想我会分享。注意:我使用的是 PHPMailer v5.1。
When you instantiate the PHPMailer class, it takes one optional argument, $exceptions. That tells PHPMailer if it should throw exceptions if it encounters any. It defaults to false which means it does not throw any exceptions, just echoes its messages out. However, if you call it like
当您实例化 PHPMailer 类时,它接受一个可选参数 $exceptions。这会告诉 PHPMailer 是否应该在遇到异常时抛出异常。它默认为 false,这意味着它不会抛出任何异常,只是将其消息回显出来。但是,如果你称之为
$mail = new PHPMailer(true);
you will tell it to throw exceptions. You can then catch those exceptions and deal with them however you choose. To me, that is much cleaner and more elegant than messing with the source code or disabling the error reporting.
你会告诉它抛出异常。然后,您可以捕获这些异常并以您选择的方式处理它们。对我来说,这比弄乱源代码或禁用错误报告更简洁、更优雅。
回答by sbuck
This is the way PHPMailer wants you to do it; does not involve editing the original class file.
这就是 PHPMailer 希望您这样做的方式;不涉及编辑原始类文件。
$mail->SMTPDebug = false;
$mail->do_debug = 0;
回答by Federico Molnar
There's a better solution (I think :) ), it's an improved version of the solution sugested by Riccardo
有一个更好的解决方案(我认为 :) ),它是 Riccardo 建议的解决方案的改进版本
In the file "class.phpmailer.php", within the function
在文件“class.phpmailer.php”中,在函数内
public function Send()
find the line
找到线
echo $e->getMessage()."\n";
and replace it for this
并为此替换它
if ($SMTPDebug)
echo $e->getMessage()."\n";
In this way, it will only show the exceptions messages (or error messages) if you are running in debug mode..
这样,如果您在调试模式下运行,它只会显示异常消息(或错误消息)。
Hope it helps!! regards!!
希望能帮助到你!!问候!!
回答by Matt Browne
Here's another solution:
这是另一个解决方案:
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
I needed to suppress errors from the JUtility::sendMail function in Joomla, which uses PHPMailer behind the scenes, so the above answers didn't work for me but output buffering did the trick.
我需要抑制 Joomla 中 JUtility::sendMail 函数的错误,它在幕后使用 PHPMailer,所以上述答案对我不起作用,但输出缓冲可以解决问题。
回答by bheatcoker
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
回答by stevebaros
There are two ways that work pretty much the same as of May 2020
有两种工作方式与 2020 年 5 月几乎相同
$mail->SMTPDebug = 0;
OR
$mail->SMTPDebug = false;
回答by Pak Ho Cheung
Delete below code if included
如果包含,请删除以下代码
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
回答by Navi
I'm having a problem with the error: Can not modify header information - headers already sent by (output started at /phpMailer/class.phpmailer.php) because my return json needs header ('Content-Type: application / json') ; And was giving error because the class class.phpmailer.php printed on the screen the error Invalid address: which generates the error of the header so to solve it was simple, it only includes in the class class.phpmailer.php in the following line the debug check and it worked Perfectly follows
我遇到了错误的问题:无法修改标题信息 - 标题已经发送(输出开始于 /phpMailer/class.phpmailer.php)因为我的返回 json 需要标题('Content-Type: application / json') ; 并给出错误,因为类 class.phpmailer.php 打印在屏幕上的错误 Invalid address: 生成标题的错误,因此解决它很简单,它只包含在以下行的 class.phpmailer.php 类中调试检查,它工作完美如下
This line
这条线
Echo $ this-> Lang ('invalid_address'). ':'. $ Address;
Change to:
改成:
If ($ this-> SMTPDebug) {
?????? Echo $ this-> Lang ('invalid_address'). ':'. $ Address;
? }
回答by Steven
If the errors are coming from the call to $mail->Send();
then you can do something like this:
如果错误来自调用 to$mail->Send();
那么您可以执行以下操作:
$oldErrorSetting = error_reporting(0); // temporarily disable warnings
$delivery = $mail->Send();
error_reporting($oldErrorSetting); // restore error setting
This will suppress PHP errors that come from the Send call.
这将抑制来自 Send 调用的 PHP 错误。
Then, to get PHPMailer's errors and do something nice with them, you can use the $delivery
return value and ErrorInfo
:
然后,要获取 PHPMailer 的错误并对其进行处理,您可以使用$delivery
返回值和ErrorInfo
:
if (!$delivery) {
$errorDetails = $mail->ErrorInfo;
print "<p>$errorDetails</p>\n";
}