Need help resolving a Mail error

Job ID: 37043318

Budget: $10 – $30 USD

When trying to connect to a mail server to send mail using a simple PHP program, I am getting an error I cannot resolve:

Email could not be sent. Error: SMTP Error: Could not connect to SMTP host. Connection failed. stream_socket_enable_crypto(): Peer certificate CN=`mail.protection.outlook.com' did not match expected CN=`smtp.domain.com'SMTP server error: QUIT command failed

Does anyone know what is causing this error?
Does anyone know how to resolve this error?

Here is the PHP code being used:

$mail = new PHPMailer(true);
try {
$mail->isSMTP();
$mail->Host = 'smtp.domain.com';
// $mail->Host = 'domain-com.mail.protection.outlook.com';
$mail->Port = 25; // Use port 25 for non-secure connection
// $mail->Port = 587; // standard port for encryption
$mail->SMTPAuth = true;
$mail->Username = 'admin(at)domain.com';
$mail->Password = 'password';
// No need to set SMTPSecure for non-secure connection
// $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
// $mail->SMTPSecure = 'none'; // Set to 'none' for non-secure connection

$send2email = "test(at)domain.com";
$mail->ClearAllRecipients(); // clear all
$send2email = filter_var($send2email, FILTER_SANITIZE_EMAIL);
$mail->addAddress($send2email);

$mail->setFrom('admin(at)domain.com', 'Admin');
$mail->addReplyTo('admin(at)domain.com');
$mail->addAddress('admin(at)domain.com','Recipient Name');
$mail->isHTML(true); // Set email format to HTML

// Set email content
$mail->Subject = 'Test Email from PHPMailer';
$mail->Body = 'This is a test email sent using PHPMailer!';

// Send the email
$mail->send();
echo 'Email sent successfully!';
} catch (Exception $e) {
echo 'Email could not be sent. Error: ', $mail->ErrorInfo;
}

This is run on a Windows Server 2019 with PHP and MySQL.
Related categories: PHP Windows Server Email Handling Email Developer