What are the best practices for testing SMTP connectivity when using PHPMailer?

When using PHPMailer to send emails, it is important to test SMTP connectivity to ensure that emails can be successfully sent. One way to do this is by using the `SMTPDebug` property in PHPMailer to output debugging information, which can help identify any issues with the SMTP connection.

use PHPMailer\PHPMailer\PHPMailer;

// Create a new PHPMailer instance
$mail = new PHPMailer();

// Enable debugging output
$mail->SMTPDebug = 2;

// Set SMTP server settings
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'your_smtp_username';
$mail->Password = 'your_smtp_password';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;

// Test SMTP connectivity
if(!$mail->smtpConnect()){
    echo 'SMTP connection failed.';
} else {
    echo 'SMTP connection successful.';
}