phpmailerHow can I catch and handle a PHPMailer exception?
Catching and handling a PHPMailer exception can be done using a try/catch
statement.
For example:
try {
// code that may throw an exception
$mail->send();
} catch (Exception $e) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
This will output:
Message could not be sent.
Mailer Error: SMTP connect() failed.
Code explanation
try
- The try block contains the code that may throw an exception.catch
- The catch block contains the code that will be executed when the exception is thrown.Exception $e
- The$e
variable is an instance of theException
class that contains information about the exception that was thrown.$mail->send()
- This is the code that may throw an exception.echo 'Message could not be sent.'
- This code is executed if an exception is thrown.echo 'Mailer Error: ' . $mail->ErrorInfo;
- This code is executed if an exception is thrown and will print the error message.
For more information, please see the PHPMailer documentation.
More of Phpmailer
- How do I use PHPMailer to attach a ZIP file?
- How can I configure PHPMailer to support Polish characters?
- How do I use PHPMailer to embed an image in an email using Base64 encoding?
- How do I determine which version of PHPMailer I'm using?
- How can I resolve the issue of my PHPmailer username and password not being accepted?
- How do I configure PHPMailer to use TLS 1.2?
- How do I configure the timeout settings for PHPMailer?
- How do I use PHPMailer with OAuth2 authentication for Microsoft accounts?
- How can I use PHPMailer SMTPDebug to debug my email sending process?
- How do I disable STARTTLS in PHPMailer?
See more codes...