You need to enable JavaScript to run this app. 使用XAMPP搭配PHPMailer发Gmail邮件提示无法连接SMTP主机报错
优惠活动
大模型
产品
解决方案
定价
更多

使用XAMPP搭配PHPMailer发Gmail邮件提示无法连接SMTP主机报错

问题描述

我最近几天在XAMPP环境中调试邮件发送功能时遇到问题,尝试使用PHPMailer实现邮件发送功能,但一直无法正常运行,报错信息如下:

2021-06-30 08:29:38 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP b25sm12430661ios.36 - gsmtp
2021-06-30 08:29:38 CLIENT -> SERVER: EHLO localhost
2021-06-30 08:29:38 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [157.230.55.84]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250 SMTPUTF8
2021-06-30 08:29:38 CLIENT -> SERVER: STARTTLS
2021-06-30 08:29:38 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
SMTP Error: Could not connect to SMTP host.
2021-06-30 08:29:39 CLIENT -> SERVER: QUIT
2021-06-30 08:29:39 SERVER -> CLIENT:
2021-06-30 08:29:39 SMTP ERROR: QUIT command failed:
SMTP Error: Could not connect to SMTP host.

Fatal error: Uncaught PHPMailer\PHPMailer\Exception: SMTP Error: Could not connect to SMTP host. in C:\xampp\htdocs\PHPMailer\vendor\phpmailer\phpmailer\src\PHPMailer.php:2136 Stack trace: #0 C:\xampp\htdocs\PHPMailer\vendor\phpmailer\phpmailer\src\PHPMailer.php(1960): PHPMailer\PHPMailer\PHPMailer->smtpConnect(Array) #1 C:\xampp\htdocs\PHPMailer\vendor\phpmailer\phpmailer\src\PHPMailer.php(1638): PHPMailer\PHPMailer\PHPMailer->smtpSend('Date: Wed, 30 J...', 'This is the HTM...') #2 C:\xampp\htdocs\PHPMailer\vendor\phpmailer\phpmailer\src\PHPMailer.php(1471): PHPMailer\PHPMailer\PHPMailer->postSend() #3 C:\xampp\htdocs\PHPMailer\index.php(37): PHPMailer\PHPMailer\PHPMailer->send() #4 {main} thrown in C:\xampp\htdocs\PHPMailer\vendor\phpmailer\phpmailer\src\PHPMailer.php on line 2136

使用的PHP代码如下:

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


if(isset($_POST['submit'])){
    // Load Composer's autoloader
    require 'vendor/autoload.php';

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

    //Server settings
    $mail->SMTPDebug = 2;                                       // Enable/disable verbose debug output, change this to 2 if you want to see it doing its thing :)
    $mail->isSMTP();                                            // Send using SMTP
    $mail->Host       = 'smtp.gmail.com';                       // Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
    $mail->Username   = 'test@gmail.com';                 // SMTP username
    $mail->Password   = '123456abc';                        // SMTP password
    $mail->SMTPSecure = 'tls';                                  // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
    $mail->Port       = 587;                                    // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

    $mail->setFrom('no-reply@example.com', 'Alex');     // From address

    $mail->addAddress('test@gmail.com');                  // Add a recipient, In this case your email address 

    // Content
    $mail->isHTML(true);                                        // Set email format to HTML
    $mail->Subject = 'Testing Email';
    // $mail->Body    = 'Name: ' .$_POST['name']. '<br>Email: ' .$_POST['email']. '<br>Message: ' .$_POST['message'];
    $mail->Body    = 'This is the HTML body';


    $mail->send();
    echo '<h2 style="color:red">Thank you '.$_POST['name'].', your message has been sent successfully</h2>';
}

已尝试的操作:

  • 按照教程步骤逐步配置邮件功能,仍无法向目标邮箱发送邮件
  • 已开启Gmail的低安全性应用访问权限
解决方案

1. 修正发件人地址配置

Gmail SMTP不允许使用与验证账号不一致的发件人地址,需要将setFrom的地址修改为和$mail->Username一致的Gmail地址:

// 修改前
$mail->setFrom('no-reply@example.com', 'Alex');
// 修改后
$mail->setFrom('test@gmail.com', 'Alex');

2. 更换Gmail验证方式

谷歌已于2022年正式停用低安全性应用访问功能,原有密码验证方式已经失效,需要按以下步骤操作:

  • 先给你的Gmail账号开启两步验证
  • 在谷歌账号的安全设置中生成应用专用密码
  • 用生成的16位应用专用密码替换代码中$mail->Password的原有密码

3. 开启PHP OpenSSL扩展

XAMPP默认可能未开启OpenSSL扩展,这是TLS加密连接的必要依赖:

  • 打开XAMPP控制面板,点击Apache行的Config按钮,选择php.ini
  • 搜索extension=openssl,去掉行首的分号注释
  • 重启Apache服务生效

4. 新增SSL兼容配置(本地测试用)

本地开发环境如果存在SSL证书验证问题,可以增加SMTP SSL选项跳过验证(仅本地测试使用,生产环境不要关闭验证):

$mail->SMTPOptions = array(
    'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
    )
);

把这段代码加在$mail->Port = 587;之后即可。

5. 检查端口连通性

确认本地防火墙、杀毒软件没有拦截对外的587端口请求,也可以尝试更换为465端口+SMTPS加密方式:

$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mail->Port = 465;

内容的提问来源于stack exchange,提问作者Fatt Sky

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.14 16:36:03