【发布时间】:2020-08-30 06:26:10
【问题描述】:
在我的页面上,我有一个使用 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 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 加密可能是权宜之计
【问题讨论】:
-
您应该首先查找与 PHPMailer 相关的任何问题是the troubleshooting guide。 PHPMailer 对 TLS 的使用与您网页的 TLS 配置完全无关,但如果您的 PHP 安装支持它,它可以与 TLSv1.3 一起正常工作。我还建议您学习如何使用作曲家。