I'm trying to use PHPMailer for a small project, but I'm a bit confused about error handling with this software. Hoping someone has experience with it. When I've set up an email and I use:
$result = $mail->Send(); if(!$result) { // There was an error // Do some error handling things here } else { echo "Email successful"; }
Which works fine, more or less. The problem is when there's an error, PHPMailer also seems to echo the error out, so if there's a problem, it just sends that info directly to the browser, essentially breaking any error handling I"m trying to do.
Is there a way to silence these messages? Its not throwing an exception, its just printing out the error, which in my test case is:
invalid address: @invalid@email You must provide at least one recipient email address.
Its meant to be an error, but it should be residing in $mail->ErrorInfo; not being echo'd out by the software.
There are many popular cases for the failure of SMTP connection in PHPMailer and lack of SSL is one of that too. There might be a case, that the Open SSL extension is not enabled in your php. ini which is creating the connection problem. So, once you enable the extension=php_openssl.
PHPMailer doesn't create/use any SQL itself, nor does it have anything to do with javascript, so it's secure on those fronts.
PHPMailer can use a non-local mail server (SMTP) if you have authentication. Further advantages include: It can print various kinds of error messages in more than 40 languages when it fails to send an email. It has integrated SMTP protocol support and authentication over SSL and TLS.
PHPMailer uses Exceptions. Try to adopt the following code:
require_once '../class.phpmailer.php'; $mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch try { $mail->AddReplyTo('[email protected]', 'First Last'); $mail->AddAddress('[email protected]', 'John Doe'); $mail->SetFrom('[email protected]', 'First Last'); $mail->AddReplyTo('[email protected]', 'First Last'); $mail->Subject = 'PHPMailer Test Subject via mail(), advanced'; $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically $mail->MsgHTML(file_get_contents('contents.html')); $mail->AddAttachment('images/phpmailer.gif'); // attachment $mail->AddAttachment('images/phpmailer_mini.gif'); // attachment $mail->Send(); echo "Message Sent OK\n"; } catch (phpmailerException $e) { echo $e->errorMessage(); //Pretty error messages from PHPMailer } catch (Exception $e) { echo $e->getMessage(); //Boring error messages from anything else! }
You can get more info about the error with the method $mail->ErrorInfo
. For example:
if(!$mail->send()) { echo 'Message could not be sent.'; echo 'Mailer Error: ' . $mail->ErrorInfo; } else { echo 'Message has been sent'; }
This is an alternative to the exception model that you need to active with new PHPMailer(true)
. But if can use exception model, use it as @Phil Rykoff answer.
This comes from the main page of PHPMailer on github https://github.com/PHPMailer/PHPMailer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With