Problem sending email from PHP with SMTPS and PhpMailer

I’m trying to modify the mail system of a website to use entirely standard SMTPS with a straightforward script which would be this:

<?php
require "vendor/phpmailer/phpmailer/PHPMailerAutoload.php";

try {
    $mail = new PHPMailer();
    $mail->SMTPDebug = SMTP::DEBUG_CONNECTION;
    $mail->Mailer = 'smtp';
    $mail->Host = 'mydomain.com';
    $mail->SMTPAuth = true;
    $mail->Username = '[email protected]';
    $mail->Password = 'my-password';
    $mail->SMTPSecure = 'ssl';
    $mail->From = '[email protected]';
    $mail->FromName = 'TEST';
    $mail->ConfirmReadingTo = '[email protected]';
    $mail->Port = 465;
    $mail->Timeout = 30;
    $mail->IsHTML(false);
    $mail->CharSet = 'UTF-8';
    $mail->addAddress('[email protected]');
    $mail->Subject = 'TEST';
    $mail->Body = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';

    $sent = $mail->Send();
    echo $sent ? "SENT" : "NOT SENT";
} catch (Exception $e) {
    echo "ERROR";
}

This script works on all the machines we have tested and against any email account. It is as simple as possible.

The error it throws is as follows:

2023-05-12 04:26:37 Connection: opening to ssl://mydomain.com:465, timeout=30, options=array ()
2023-05-12 04:26:38 Connection failed. Error #2: stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages:error:140943FC:SSL routines:ssl3_read_bytes:sslv3 alert bad record mac [E:wwwAPPvendorphpmailerphpmailerclass.smtp.php line 298]
2023-05-12 04:26:38 Connection failed. Error #2: stream_socket_client(): Failed to enable crypto [E:wwwAPPvendorphpmailerphpmailerclass.smtp.php line 298]
2023-05-12 04:26:38 Connection failed. Error #2: stream_socket_client(): unable to connect to ssl://mydomain.com:465 (Unknown error) [E:wwwAPPvendorphpmailerphpmailerclass.smtp.php line 298]
2023-05-12 04:26:38 SMTP ERROR: Failed to connect to server: (0)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
NOT SENT

Well, after several days and more than 20 hours, I can’t find what the problem is, although I’m sure it’s something directly related to Windows 11. Neither PHP nor the SSL libraries have anything to do with it.

I have done a lot of tests:

  • Minimal script with various libraries on the market; none of them works; they all give exactly the same error.
  • I’ve tested that script on several Windows 10 and 11 machines, and it works on several Linux, and it works on my Mac, and it works on all except this server with Windows 11.
  • I have checked the Windows SSL and TLS options; they are identical on both DEV and PROD machines.
  • I have also tried connecting via Telnet to see if it throws any messages, but nothing.
  • I have tried running the test script from the command line, and the result is the same.
  • I have created an email account on a server of mine, and tried using it to send; the problem is the same.
  • I have forced the SMTP connection with SSL2, SSL3, TLS1.0, TLS1.1, TLS1.2 and TLS1.3 with all of them giving the same error.
  • I have deactivated the antivirus, and the same result. At this point, I have tried to open a case in PHPmailer, but they have rejected it because we are using an obsolete version as it is the only one that supports PHP 7.1, which is what the web uses, so we are stuck.

Any help will be appreciated.