How can PHP developers troubleshoot TLS negotiation issues between PHPMailer and SMTP servers to ensure secure email transmission?

To troubleshoot TLS negotiation issues between PHPMailer and SMTP servers, PHP developers can ensure that the SMTP server supports TLS encryption and that the correct port (usually 587) is being used for secure email transmission. They can also check if the PHPMailer library is properly configured to use TLS encryption by setting the 'SMTPSecure' parameter to 'tls'.

// Instantiate PHPMailer
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

$mail = new PHPMailer(true);

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

// Additional settings
$mail->setFrom('your@example.com', 'Your Name');
$mail->addAddress('recipient@example.com', 'Recipient Name');
$mail->Subject = 'Test Email';
$mail->Body = 'This is a test email';

// Send email
if ($mail->send()) {
    echo 'Email sent successfully';
} else {
    echo 'Error sending email: ' . $mail->ErrorInfo;
}