PHP Mailer-454 TLS连接失败
在我的页面上,我有一个使用PHP Mailer的联系人表单。 一开始,我只是出于测试目的,把它和我的Gmail账户一起使用。一切都很顺利。然后我决定将电子邮件服务从Gmail改为我的主机提供商提供的服务。然后问题就开始了。每次我尝试发送电子邮件时,都会发生异常:PHP Mailer-454 TLS连接失败,php,email,ssl,phpmailer,Php,Email,Ssl,Phpmailer,在我的页面上,我有一个使用PHP Mailer的联系人表单。 一开始,我只是出于测试目的,把它和我的Gmail账户一起使用。一切都很顺利。然后我决定将电子邮件服务从Gmail改为我的主机提供商提供的服务。然后问题就开始了。每次我尝试发送电子邮件时,都会发生异常: 2020-05-13 20:53:44 CLIENT -> SERVER: STARTTLS 2020-05-13 20:53:44 SERVER -> CLIENT: 220 ready for tls SMTP Erro
2020-05-13 20:53:44 CLIENT -> SERVER: STARTTLS
2020-05-13 20:53:44 SERVER -> CLIENT: 220 ready for tls
SMTP Error: Could not connect to SMTP host.
2020-05-13 20:53:44 CLIENT -> SERVER: QUIT
2020-05-13 20:53:44 SERVER -> CLIENT: 454 TLS connection failed: error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake failure (#4.3.0)
2020-05-13 20:53:44 SMTP ERROR: QUIT command failed: 454 TLS connection failed: error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake failure (#4.3.0)
这是我发送电子邮件的代码:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;
require 'PHPMailer.php';
require 'SMTP.php';
require 'Exception.php';
class Mailer
{
private $mail;
public function __construct($host, $user, $password, $port = 587)
{
$this->mail = new PHPMailer();
try
{
$this->mail->SMTPDebug = SMTP::DEBUG_SERVER;
$this->mail->CharSet = "UTF-8";
$this->mail->isSMTP();
$this->mail->Host = $host;
$this->mail->SMTPAuth = true;
$this->mail->SMTPSecure = 'ssl';
$this->mail->Username = $user;
$this->mail->Password = $password;
$this->mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$this->mail->Port = $port;
}
catch(Exception $e)
{
echo "Exception: " . $e;
}
}
public function Send($from, $alias, $to, $subject, $body, $cc)
{
try
{
$this->mail->setFrom($from, $alias);
$this->mail->addAddress($to);
$this->mail->isHTML(true);
$this->mail->Subject = $subject;
$this->mail->Body = $body;
if($cc !== '')
{
$this->mail->AddCC($cc);
}
if(!$this->mail->send())
{
die($this->mail->ErrorInfo);
}
return true;
}
catch (Exception $e)
{
return false;
}
}
当涉及到TLS/SSL时,我想我配置PHP Mailer是错误的,因为我对这一点非常熟悉。我的网页使用TLS 1.3加密可能是一个权宜之计,这里有一个提示,如果它有效,不要忘记投票加强。让我们转到设置
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => false
)
);
请记住,这是一个暂时的解决办法,可以立即工作,但我建议调查这个问题。另一个细节是使用端口465和587进行测试,有时可能是这样
$mail->Port = "587";
$mail->SMTPSecure = "tls";
还有一件事我不明白。。。为什么我要使用$this->mail->SMTPSecure两次
换端口的把戏成功了。完美的简单!非常感谢:当然,设置SMTPSecure两次是我的疏忽。这个提示很有用。成功请不要建议禁用证书验证是一种解决方案,它只是鼓励其他人在实际情况变得更糟时也这样做,隐藏可能更大的问题。您应该首先查找与PHPMailer相关的任何问题。PHPMailer对TLS的使用与web页面的TLS配置完全无关,不过如果您的PHP安装支持TLSv1.3,它可以很好地工作。我还建议您学习如何使用composer。
$this->mail->SMTPSecure = 'ssl';
$this->mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;