【问题标题】:problem with PHPMAILER Could not connect to SMTP hostPHPMAILER 的问题无法连接到 SMTP 主机
【发布时间】:2019-09-18 17:35:39
【问题描述】:

我有 PHPMAILER 的问题,它在 localhost 中运行良好,但在服务器中给我错误

PHPMAILER 代码

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

function sendMail($driver, $driverUser){

    // Load Composer's autoloader
    require 'vendor/autoload.php';

    // Instantiation and passing `true` enables exceptions
    $mail = new PHPMailer(true);

    try {
        //Server settings
        $mail->SMTPDebug = 2;                                       // Enable verbose debug output
        $mail->isSMTP();                                            // Set mailer to use SMTP
        $mail->Host       = 'smtp.gmail.com';                       // Specify main and backup SMTP servers
        $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
        $mail->Username   = 'wastaapplication@gmail.com';                // SMTP username
        $mail->Password   = '*******';                      // SMTP password
        $mail->SMTPSecure = 'tls';                                  // Enable TLS encryption, `ssl` also accepted
        $mail->Port       = 587;                                    // TCP port to connect to

        //Recipients
        $mail->setFrom('wastaapplication@gmail.com', 'Wasta Driver');
        $mail->addAddress($driver, $driverUser);     // Add a recipient

        // Content
        $mail->isHTML(true);                                  // Set email format to HTML
        $mail->Subject = 'New Order';
        $mail->Body    = ' مرحبا ' . '<strong>' . $driverUser . '</strong>' . '  لديك طلبيه جديده برجاء مراجعه برنامج الطيارين ';
        $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}";
    }

}

错误

2019-04-30 05:14:51 SERVER -> CLIENT: 220-server.issgroups.org ESMTP Exim 4.91 #1 Tue, 30 Apr 2019 07:14:51 +0200 220-We do not authorize the use of this system to transport unsolicited, 220 and/or bulk e-mail.
2019-04-30 05:14:51 CLIENT -> SERVER: EHLO wastetkheer.com
2019-04-30 05:14:51 SERVER -> CLIENT: 250-server.issgroups.org Hello wastetkheer.com [138.201.107.252]250-SIZE 52428800250-8BITMIME250-PIPELINING250-AUTH PLAIN LOGIN250-STARTTLS250 HELP
2019-04-30 05:14:51 CLIENT -> SERVER: STARTTLS
2019-04-30 05:14:51 SERVER -> CLIENT: 220 TLS go ahead
SMTP Error: Could not connect to SMTP host.
2019-04-30 05:14:51 CLIENT -> SERVER: QUIT
2019-04-30 05:14:51 
2019-04-30 05:14:51 
SMTP Error: Could not connect to SMTP host.
Message could not be sent. Mailer Error: SMTP Error: Could not connect to SMTP host.

我正在尝试将 SMTPDebug 更改为 1 & //$mail->isSMTP();

它在本地主机而不是在服务器上运行良好 我想问题出在 SMTP 身份验证上,但我找不到问题。

【问题讨论】:

  • 您使用的是什么服务器?一些托管公司会阻止 SMTP 端口并强制您使用他们的 SMTP 中继(如 GoDaddy)。此外,如果您收到“无法连接到 SMTP 主机”,很可能不是身份验证(因为这需要与主机建立连接)。

标签: php phpmailer


【解决方案1】:

发帖前请先搜索,因为这个问题已经回答过很多次了。

在 STARTTLS 之后立即失败,表示 TLS 错误。这在 gmail 中很常见,因为他们的 CA 根证书大约在一年前更改为许多旧操作系统中不存在的证书。它在 localhost 上为您工作,因为您的本地操作系统没有过时的 CA 证书。

阅读the troubleshooting guide,它会告诉你如何处理这个问题。

也可能是您的 ISP 将 SMTP 流量重定向到他们自己的邮件服务器,导致证书名称不匹配 - 该指南提供了诊断确切问题的方法。

【讨论】:

    【解决方案2】:

    尝试替换 $mail->SMTPSecure = 'tls';$mail->SMTPSecure = 'ssl';

    //因为你的服务器在这个错误之后退出了: 2019-04-30 05:14:51 服务器-> 客户端:220 TLS 继续 SMTP 错误:无法连接到 SMTP 主机。

    那就试试吧

    【讨论】:

    • 连接服务器失败
    • 这将不起作用,因为隐式 TLS 模式(设置 SMTPSecure = 'ssl' 时得到的)在端口 587 上不起作用。内部错误将保持不变,您将获得更少的关于什么的信息原因是。
    猜你喜欢
    • 2012-05-24
    • 2018-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多