【问题标题】:PHP Mailer - 454 TLS connection failedPHP Mailer - 454 TLS 连接失败
【发布时间】: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 一起正常工作。我还建议您学习如何使用作曲家。

标签: php email ssl phpmailer


【解决方案1】:

这里有一个提示,如果它有效,不要忘记投票加强。让我们去设置...

$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 两次?

 $this->mail->SMTPSecure = 'ssl';
 $this->mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;

【讨论】:

  • 端口更改技巧完成了这项工作。完美的简单!非常感谢 :) 当然,两次设置 SMTPSecure 是我的疏忽。
  • 小费帮了大忙。成功!
  • 请不要暗示禁用证书验证是一种解决方案,它只会鼓励其他人在实际上使情况变得更糟时也这样做,从而隐藏可能更大的问题。
猜你喜欢
  • 1970-01-01
  • 2015-01-04
  • 1970-01-01
  • 2021-02-02
  • 2014-10-05
  • 2016-03-19
  • 2012-08-30
  • 1970-01-01
  • 2021-02-10
相关资源
最近更新 更多