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."
My code: http://pastie.org/1497819
Thanks
This is not probably the best solution, but it works.
In your phpmailer library folder open "class.phpmailer.php",
find
public function Send()
inside it comment the line
echo $e->getMessage()."\n";
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.
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
$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.
This is the way PHPMailer wants you to do it; does not involve editing the original class file.
$mail->SMTPDebug = false;
$mail->do_debug = 0;
There's a better solution (I think :) ), it's an improved version of the solution sugested by Riccardo
In the file "class.phpmailer.php", within the function
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!!
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
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.
If the errors are coming from the call to $mail->Send();
then you can do something like this:
$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.
Then, to get PHPMailer's errors and do something nice with them, you can use the $delivery
return value and ErrorInfo
:
if (!$delivery) {
$errorDetails = $mail->ErrorInfo;
print "<p>$errorDetails</p>\n";
}
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
This line
Echo $ this-> Lang ('invalid_address'). ':'. $ Address;
Change to:
If ($ this-> SMTPDebug) {
Echo $ this-> Lang ('invalid_address'). ':'. $ Address;
}
来源:https://stackoverflow.com/questions/4798498/disable-phpmailer-error-messages