What are the best practices for handling SSL/TLS certificates when configuring Google SMTP with PHPMailer in PHP?

When configuring Google SMTP with PHPMailer in PHP, it is important to properly handle SSL/TLS certificates to ensure secure communication. One best practice is to explicitly set the SMTPSecure option to 'ssl' and enable SMTP authentication with your Google account credentials. Additionally, you may need to specify the SMTP server host as 'smtp.gmail.com' and the port as 465.

// Include PHPMailer library
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

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

// SMTP settings for Google SMTP
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'your@gmail.com';
$mail->Password = 'your_password';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;

// Set the sender and recipient
$mail->setFrom('your@gmail.com', 'Your Name');
$mail->addAddress('recipient@example.com', 'Recipient Name');

// Set email content
$mail->isHTML(true);
$mail->Subject = 'Subject';
$mail->Body = 'Email body';

// Send the email
if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}