是否可以指定 PHPMailer 中使用的加密协议版本?
尝试构建一个用于测试 SMTP 配置的小型 Web 工具。我曾经能够在 .NET 中指定协议版本,但现在我使用的是 apache,我正在尝试使用 phpmailer 在 PHP 页面中执行此操作。所以我需要让它只尝试一个单一的加密版本,比如 TLS 1.3。
我知道我可以将 smtpautoTLS 设置为 FALSE。但是我可以在 SMTPOptions 数组中指定 TLS 1.3 或 SSL v3 吗?我似乎无法在文档/示例/谷歌中找到它。
谢谢!
编辑更新代码并确认此代码仅适用于 SMTPS/隐式样式,但不适用于 STARTTLS
<?php
require_once 'PHPMailer/vendor/autoload.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
$from = $_POST['from'];
$to = $_POST['to'];
$subj = $_POST['subj'];
$body = $_POST['body'];
$server = $_POST['addr'];
$port = $_POST['port'];
$auth = $_POST['auth'];
$enctype = $_POST['enctype'];
$encver = $_POST['encver'];
$authuser = $_POST['authuser'];
$authpw = $_POST['authpw'];
$mail = new PHPMailer(true);
$mail->IsSMTP();
//$mail->SMTPDebug = 2; //2 for debugging with server responses. 0-4 as choices
$smtpopts = array();
if($encver == "auto")
{
$mail->SMTPAutoTLS = true;
}
else
{
$mail->SMTPAutoTLS = false;
}
if($auth == 1)
{
$mail->SMTPAuth = true;
$mail->Username = $authuser;
$mail->Password = $authpw;
switch($enctype)
{
case "implicit":
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
break;
case "explicit":
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
break;
}
switch($encver)
{
case "ssl3_0":
$smtpopts['ssl'] = array('crypto_method' => STREAM_CRYPTO_METHOD_SSLv3_CLIENT);
break;
case "tls1_0":
$smtpopts['ssl'] = array('crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT);
break;
case "tls1_1":
$smtpopts['ssl'] = array('crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT);
break;
case "tls1_2":
$smtpopts['ssl'] = array('crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT);
break;
case "tls1_3":
$smtpopts['ssl'] = array('crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT);
break;
}
$mail->SMTPOptions = $smtpopts;
}
else
{
$mail->SMTPAuth = false;
}
$mail->Host = $server;
$mail->Port = $port;
$mail->SetFrom($from);
$mail->AddAddress($to);
$mail->Subject = $subj;
$mail->MsgHTML($body);
$response = array();
try
{
header('Content-Type: application/json');
$mail->Send();
$response['success'] = 1;
$response['msg'] = "Success";
echo json_encode($response);
}
catch (Exception $e)
{
$response['success'] = 0;
$response['msg'] = $e->errorMessage();
error_log($e->errorMessage());
echo json_encode($response);
}
catch (\Exception $e)
{
$response['success'] = -99;
$response['msg'] = $e->getMessage();
error_log($e->getMessage());
echo json_encode($response);
}
?>