Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SMTP TLS on Windows not working (hang forever) #2958

Closed
Maloupi opened this issue Sep 29, 2023 · 2 comments
Closed

SMTP TLS on Windows not working (hang forever) #2958

Maloupi opened this issue Sep 29, 2023 · 2 comments

Comments

@Maloupi
Copy link

Maloupi commented Sep 29, 2023

Problem description

On windows (PHP 7.4 -> PHP 8.2) (no problem on Linux), sending a mail via SMTP with TLS (no problem with SSL) hang forever on stream_socket_enable_crypto in startTLS() in SMTP.php

I think this is because it use tls 1.0 first.
if i force crypto_method to tls 1.1 or 1.2, it works well :

$crypto_method = STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT;

Code to reproduce


define('SMTP_SERVER', '**********');
define('SMTP_PORT', '587');
define('SMTP_ENCRYPTION', 'tls');
define('SMTP_ACCOUNT', '**********');
define('SMTP_PASSWORD', '**********');
define('DEST_ADDRESS', '**********');

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'phpmailersrc/Exception.php';
require 'phpmailersrc/PHPMailer.php';
require 'phpmailersrc/SMTP.php';

$mail = new PHPMailer(true);

try {
    $mail->isSMTP();                      
    $mail->Host       = SMTP_SERVER;      
    $mail->SMTPAuth   = true;             
    $mail->Username   = SMTP_ACCOUNT;     
    $mail->Password   = SMTP_PASSWORD;    
    $mail->SMTPSecure = SMTP_ENCRYPTION;  
    $mail->Port       = SMTP_PORT;    
    
    $mail->setFrom(SMTP_ACCOUNT);
    $mail->addAddress(DEST_ADDRESS);
    $mail->isHTML(true);    
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
@Synchro
Copy link
Member

Synchro commented Sep 29, 2023

TLS protocols don't work like that; it doesn't try different versions in order, but presents all the options as a list. It may be that your Windows machine has a buggy TLS implementation that breaks on some configs. There isn't a built-in way to change this, but you can achieve it by subclassing the SMTP class (like in this example), overriding the startTLS method, and then injecting your instance back into PHPMailer using setSMTPInstance().

Short version:

class mySMTP extends SMTP
{
    public function startTLS()...
}

$mail = new PHPMailer();
$mail->setSMTPInstance(new mySMTP());

You can then carry on as normal.

Note to myself; I need to update the override example to use setSMTPInstance()

@Synchro Synchro closed this as completed Sep 29, 2023
@Maloupi
Copy link
Author

Maloupi commented Sep 29, 2023

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants