You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SMTP connect()调用失败求助——附Gmail SMTP交互日志

Alright, let's dig into your PHPMailer SMTP issue. Looking at your interaction logs, the connection gets through the initial SMTP handshake, confirms STARTTLS is ready, then fails—this points directly to a problem during the TLS handshake phase, not the initial SMTP connection itself.

First, let's recap the key part of your logs for clarity:

2018-03-05 18:25:08 服务器 -> 客户端: 220 smtp.gmail.com ESMTP o5sm27821483pfh.51 - gsmtp
2018-03-05 18:25:08 客户端 -> 服务器: EHLO localhost
2018-03-05 18:25:08 服务器 -> 客户端: 250-smtp.gmail.com at your service, [43.247.156.6]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2018-03-05 18:25:08 客户端 -> 服务器: STARTTLS
2018-03-05 18:25:08 服务器 -> 客户端: 220 2.0.0 Ready to start TLS
随后出现SMTP Error: Could not connect to SMTP host

Here are the most likely fixes, ordered by how common they are:

1. Check if OpenSSL extension is enabled in PHP

PHPMailer relies on OpenSSL to handle TLS encryption. If it's disabled, the TLS handshake will fail immediately after the server says "Ready to start TLS".

  • Create a quick test file phpinfo.php with this code:
    <?php phpinfo(); ?>
    
  • Access it via your browser, search for "openssl"—if you don't see "Enabled" next to "OpenSSL Support", you need to enable it:
    • Open your php.ini file, find the line ;extension=openssl, remove the leading semicolon (;), then restart your web server (Apache/Nginx) or PHP-FPM.

2. Fix CA certificate configuration

Gmail's SMTP server requires valid root CA certificates to verify the connection. Missing or outdated certs will break the TLS handshake.

For Linux servers:

  • Update your system's CA certificates package. On Debian/Ubuntu:
    sudo apt-get update && sudo apt-get install --reinstall ca-certificates
    
  • On RHEL/CentOS:
    sudo yum reinstall ca-certificates
    

For Windows servers:

  • Download the latest Mozilla CA certificate bundle (search for "Mozilla cacert.pem" to get the official file), save it to a secure location on your server.
  • Edit php.ini to add:
    openssl.cafile = "C:\path\to\your\cacert.pem"
    
  • Restart your web server.

Temporary (insecure) workaround for testing:

If you need to confirm this is the issue, you can disable certificate validation in PHPMailer (do NOT use this in production):

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

3. Verify firewall/security group rules

Your server's firewall or cloud provider's security group might be blocking outbound TLS traffic on port 587 (the standard port for STARTTLS).

  • Test the connection directly from your server using OpenSSL:
    openssl s_client -connect smtp.gmail.com:587 -starttls smtp
    
  • If this command fails to complete the handshake, you need to adjust your firewall rules to allow outbound traffic to smtp.gmail.com on port 587.

4. Fix Gmail account security settings

Gmail has strict security rules that block non-OAuth2 connections by default, especially if you have 2FA enabled.

  • If you use 2FA: Generate an App Password for your account (this is a special password for apps that don't support OAuth2). Use this App Password instead of your regular account password in PHPMailer.
  • If you don't use 2FA: Gmail may still block "less secure apps"—instead of enabling that (it's deprecated), switch to using PHPMailer's OAuth2 support. You'll need to set up a Google Cloud project, create OAuth credentials, and configure PHPMailer to use them.

5. Upgrade your PHP version

If you're running PHP 5.5 or older, it doesn't support TLS 1.2, which is required by Gmail's SMTP servers. Upgrade to PHP 5.6 or newer (preferably 7.4+ for security and performance) to get proper TLS 1.2/1.3 support.

Start with the first two fixes—they're the most common culprits in cases like this. If those don't work, move on to checking your network and Gmail settings.

内容的提问来源于stack exchange,提问作者Girish A

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 07:23:18