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."
Thanks
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;
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";
There are two ways that work pretty much the same as of May 2020
$mail->SMTPDebug = 0;
OR
$mail->SMTPDebug = false;
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;
}
Delete below code if included
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
Related
Before you classify this post as a duplicate, please note that I've been working on this issue for days and I've reviewed all of the other posts.
Previous solution: Use the latest version of PHPMailer.
I've tried PHPMailer 6.2.0, 6.1.8 , 6.1.7 and older versions
I've tried using the composer and i've also tried using PHPmailer
without the composer.
Sometimes PHPMailer works and sometimes I get the same error. There's no rhyme or reason. I can run the following script and it works with no error, run it 5 seconds later then it doesn't work, run it again 5 seconds later and it works. What the heck?!
require_once("PHPMailer-master/PHPMailerAutoload.php");
also tried:
require "PHPMailer3/PHPMailer-6.2.0/src/PHPMailer.php";
require "PHPMailer3/PHPMailer-6.2.0/src/SMTP.php";
require "PHPMailer3/PHPMailer-6.2.0/src/Exception.php";
use PHPMailer\PHPMailer\PHPMailer;
Lets continue:
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->Host = "10.10.10.38";
$mail->Port = 25;
$mail->SMTPDebug = false;
$mail->SMTPAuth = false;
$mail->SMTPSecure = false;
$mail->SetFrom ("me#x.com", "me");
$mail->AddReplyTo("me#x.com", "me");
$mail->Subject = "I need help fixing you";
$mail->AltBody = "Yes It Worked!";
$body ="Yes It Worked!";
$mail->MsgHTML($body);
$mail->AddAddress("x#x.com", "x");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
Another Previous solution: check the from address
At first i thought this was the solution because my from address was a variable but that's not it! I'm using a static from address.
Another Previous solution: the from address is using single quotes but everything else is using double quotes.
Tried that
I've also tried:
$mail->From = "me#x.com";
$mail->FromName = "me#x.com";
What else is there to try?
By the way, i'm running PHP 7.3.21 and i'm using an internal SMTP relay server. Please dont suggest using a different email server. I have to get it working with the current server.
Thanks,
The code you're using looks like it's been copied from a very old example, though I don't think that's the issue here.
require_once("PHPMailer-master/PHPMailerAutoload.php");
that file is obsolete and has been omitted from supported PHPMailer versions since 2017.
I can run the following script and it works with no error, run it 5 seconds later then it doesn't work, run it again 5 seconds later and it works.
This tells you something important – your script (and PHPMailer) is not the source of the problem. The most likely explanation is that your mail server, or a firewall between you and the mail server, has some kind of throttling or rate limiting that's preventing you sending all the time, resulting in inconsistent results.
You're also doing this:
$mail->SMTPDebug = false;
Since you're trying to debug an SMTP issue, don't you think that this might possibly be relevant? Especially since it's one of the first things referred to in the troubleshooting guide I'm sure you've seen already, and used and described in literally hundreds of answers on here. Your very first step should be to set $mail->SMTPDebug = 3; and read what the output says, and post it as part of your question.
I'm using PhpMailer to send e-mails. It's working fine.
But in some cases I don't want to wait for the send function to return, specially because it can take a while sometimes. I just want to send and finish the funcition right away.
Is it possible to achieve this?
This is a sample code I'm using (nothing different from the basics).
try {
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
// ...other options...
$mail->send();
return true;
} catch (Exception $e) {
return $e;
}
You don't need to use ajax or configure your own queueing system to do this. Just use a local mail server - which implicitly has a built-in queueing system that you don't need to configure - and you can submit messages to it in a fraction of a second. There is some performance advice on the PHPMailer wiki.
Like jeroen mentioned, you should use queues to achieve this. If you ever configured the cron to work with PHP, this have the similar principle except that it doesn't repeat the same task by schedule but executes any task you provide one-by-one in a queue. You can even give priorities to these tasks. I suggest you to start with beanstalkd.
This is driving me up a wall!
I'm trying to use phpmailer to send e-mail. Here is my code:
print "about to do the require!";
require_once("$s[phppath]/phpmailer.php");
print "require has been successful! creating a PHPMailer object!";
$mail = new PHPMailer();
print "yay!";
However, the program never gets to "yay", it dies at line 4 ($mail = new PHPMailer();). Most maddening is that no error message is displayed, even though php is running with display_errors = On. To test this, I threw in some obvious errors, like skipping semi-colons at the end of lines, and I immediately get an error message about this.
What the heck is going on here?
Edited
I modified the code like this:
require_once("$s[phppath]/phpmailer.php");
try {
$mail = new PHPMailer(true);
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
Still no dice.
I am using PHPMailer also. Now in 2018 Version 6.05.
I was also in trouble with creation of $mail = new PHPMailer; I always saw the message that the class could not be created. But I was sure the file with the class was included.
What I found out, was that there is a namespace used within the class PHPMailer.
So the new object does not exist as "PHPMailer" but as class within the namespace PHPMailer\PHPMailer ! (not to be confused with folders of your installation).
What finally worked for me was to write the following line:
$mail = new PHPMailer\PHPMailer\PHPMailer;
// You need to add the namespace part after new and before class PHPMailer
Ok, there were two parts to this question:
1) Why no error messages? I feel very, very stupid about this one, because though I had display_errors turned on, my own script actually disabled error_reporting. Thank you Pathik Ghandi for pointing out this very simple fix. I can be quite special at times.
2) Why was PHPMailer not working? Because apparently PHPMailer now requires TWO files instead of one: class.phpmailer.php is not enough. You now also need PHPMailerAutoload.php. Hope this helps someone down the line!
Try echoing out $s[phppath] to make sure you are getting the right path. Redownload and reupload the phpMailer lib. Try using require instead of require_once. Try using include instead of require. Go into the phpmailer.php file and add in an echo or something to make sure you are actually requiring that file.
To be sure; Are you running some kind of framework? ie Wordpress? If so you may need to set a flag to show PHP error messages. WP: define("WP_DEBUG", true);
Try to call restore_error_handler() function above require.
I'm running a PHP script every night using a cron service. Everything it outputs will be printed to an log file for debug prepossess. The file I use will retrieve xml's from a different site using the function 'file_get_contents()'. But the function can return an error which I really don't want to see as I am already showing a custom error.
Quick example of my piece of code:
$buffer = #file_get_contents('http://xx:xx#xx/xml/xx?offset=2') or print('retry in 5 seconds');
if($buffer === false) {
sleep(5);
$buffer = #file_get_contents('http://xx:xx#xx/xml/xx?offset=2') or print('error notice');
}
The problem is the first one will trigger an error and print it'll retry in 5 seconds. How can I correctly suppress the thrown error?
I have an error handler, but I prefer not to catch this error separately.
Edited:
My solution wasn't to change the error_reporting, but to catch the error message. If it starts with 'file_get_contents()', no error will be thrown. This is not the best way, but will do the job for me.
You can try inserting this at the start:
error_reporting(0);
Then after the code with the error/warning:
error_reporting(E_ALL ^ E_WARNING);
Okay, never ever use the #-operator.
In PHP you have two options available: either use a custom error handler or use try/catch.
Since file_get_contents doesn't throw an exception, you can only use the first approach.
You can set an error handler like this: http://php.net/set-error-handler and then act correctly (log something or return a custom error code).
If you just want to turn of all errors use error_reporting(0) or if you just want to turn off a specific category use error_reporting(E_ALL ^ E_WARNING) (all but warnings) or specifcy them explicitely error_reporting(E_WARNING | E_NOTICE) (warnings and notices).
I prefer the first approach, since when you just disable it you have no idea of what's going on in your code.
Add # before command or use try catch.
I've got a test script - test1.php:
echo "2";
include_once("api_class.php");
echo "3";
$objAPI = new API();
echo "4";
api_class.php has:
<?php
class API extends DATABASE
{
...
}
?>
However, when I access test1.php, I see only:
23
What am i doign wrong?
You didn't get the class source pasted in, but something must be wrong with your syntax. Add the following line to the top of your file and it will output verbose error messages that should point you in the correct direction.
error_reporting(E_ALL);
Most likely, there is an error in api_class.php, more specifically in the API constructor and it is failing. And you have debug/error messages turned off in PHP.
Either turn debug/error messages display on or check your server logs to find the error message.
I 'd guess that the API class expects to establish a database connection (maybe configured by constructor parameters?) and calls die if it cannot. Which is probably what happens when you try to construct a new API.