php - GMail 421 4.7.0 稍后再试,关闭连接

标签 php linux dns smtp gmail

我试图找出为什么它无法使用 GMail 从我的服务器发送邮件。为此,我使用 SwiftMailer,但我可以将问题附在以下隔离代码中:

<?php

$sock = stream_socket_client('tcp://smtp.gmail.com:587', $errno, $errstr);
if (false == $sock) {
    die('error');
}
$server_response = fread($sock, 4096);
echo $server_response . PHP_EOL;
fclose($sock);
当我在本地计算机(Windows 10、XAMPP、PHP 5.6.24)上运行此代码时,输​​出为:
220 smtp.gmail.com ESMTP qp16sm1358626ejb.89 - gsmtp

So 220 means everything is fine.

But when I run the exact same code on my server (Debian Jessie, PHP 5.6.38), the output is:

421 4.7.0 Try again later, closing connection.

GMail's error reference https://support.google.com/a/answer/3726730 doesn't give any advice for this error message.

I also tried this on the server console without PHP and it works fine too:

> nc smtp.gmail.com 587
$ 220 smtp.gmail.com ESMTP n11sm3188821wmi.15 - gsmtp

So I can delimit the problem

  • It has nothing to do with any security settings of my GMail account (because no authentification is sent at this point)
  • My server is not blacklisted on GMail (because the nc command works fine)

Are there any settings of the php.ini that can cause this error?

UPDATE

Just for fun I set up a simple TCP server on port 587 on another machine and display the IP address of any connected client. There is no different if I use the above PHP client code or connecting via nc command. I don't know what could be the different for the GMail's SMTP server while conneting via the client PHP code and the nc command.

UPDATE 2

While enabling verbose mode in the nc command, could DNS fwd/rev mismatch be a problem?

> nc -v smtp.gmail.com 587
$ DNS fwd/rev mismatch: smtp.gmail.com != wr-in-f109.1e100.net
$ smtp.gmail.com [108.177.15.109] 587 (submission) open
$ 220 smtp.gmail.com ESMTP z15sm4348737wrn.89 - gsmtp

UPDATE 3

Run the above code with

$sock = stream_socket_client('tcp://wr-in-f109.1e100.net:587', $errno, $errstr);
它有效,它以 220 响应。但问题是,SwiftMailer 在使用 stream_socket_enable_crypto() 后尝试建立 TLS 安全性。这失败了
Peer certificate CN=`smtp.gmail.com' did not match expected CN=`wr-in-f109.1e100.net'

UPDATE 4

It's getting me crazy. When I use Python's TCP sockets to connect to smtp.gmail.com, GMail responses with 220:

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('smtp.gmail.com', 587))
data = s.recv(4096)
s.close()

print data
输出:
220 smtp.gmail.com ESMTP p6sm8392243wmg.0 - gsmtp

What is the difference between Python's sockets and PHP's stream_socket_client?

Edit: The same behavior with PHP's non-streaming socket function:

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if(!is_resource($socket)) onSocketFailure("Failed to create socket");
socket_connect($socket, "smtp.gmail.com", 587)
        or onSocketFailure("Failed to connect", $socket);

$line = socket_read($socket, 1024, PHP_NORMAL_READ);

var_dump($line);
socket_close($socket);
更新 5
现在我认为这是IPv6的问题。我不是一个大 C 程序员,但我试图理解 PHP 的实现。
看一下
  • xp_socket.c函数php_tcp_sockop_connect用途 php_network_connect_socket_to_host
  • network.c在这里实现
  • 它使用 php_network_getaddresses
  • 而这个函数使用 getaddrinfo
  • php_network_connect_socket_to_host迭代主机地址列表并尝试连接到它。如果成功,它将停止循环。
    我没有用 C 尝试过,但我认为下面的 Python 代码是一样的:
    import socket
    print socket.getaddrinfo("smtp.gmail.com", 587)
    
    哪个打印
    [
     (10, 1, 6, '', ('2a00:1450:400c:c0c::6d', 587, 0, 0)),
     (10, 2, 17, '', ('2a00:1450:400c:c0c::6d', 587, 0, 0)),
     (10, 3, 0, '', ('2a00:1450:400c:c0c::6d', 587, 0, 0)),
     (2, 1, 6, '', ('108.177.15.109', 587)),
     (2, 2, 17, '', ('108.177.15.109', 587)),
     (2, 3, 0, '', ('108.177.15.109', 587))
    ]
    

    (I formatted the line for better reading)

    As you can see, there are three IPv6 addresses on the top of the list and php_network_connect_socket_to_host try these IPv6 adresses first before it comes to the IPv4 addresses.

    So what happens when I try to connect directly to the first IPv6 address?

    <?php
    
    $sock = stream_socket_client('tcp://[2a00:1450:400c:c02::6c]:587', $errno, $errstr);
    if (false == $sock) {
        die('error');
    }
    $server_response = fread($sock, 4096);
    echo $server_response . PHP_EOL;
    fclose($sock);
    
    输出是:
    421 4.7.0 稍后重试,关闭连接。

    这个SO问题与这个观察有关:Why I can't send emails using smtp.gmail.com:587 on ipv6?

    最佳答案

    发现后,当使用 IPv6 地址时会出现错误消息,黑客更喜欢 IPv4 地址。
    根据https://serverfault.com/questions/511237/linux-block-ipv6-for-certain-applications-hostnames#answer-514397最简单的方法是将以下行添加到 etc/hosts :

    108.177.15.109 smtp.gmail.com
    
    此解决方案的缺点是静态 IP 地址。 smtp.gmail.com可能会更改 IP 地址。这就是为什么我不将此答案标记为已接受的原因。
    不知道能不能配置 /etc/gai.conf 更喜欢 IPv4 而不是 IPv6 smtp.gmail.com .
    这篇文章可能很有趣,详细了解 getaddrinfo 的工作原理:https://jameshfisher.com/2018/02/03/what-does-getaddrinfo-do/

    关于php - GMail 421 4.7.0 稍后再试,关闭连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63375834/

    相关文章:

    php - 无法在PHP中下载二进制文件

    php - 在提交表单时加载跟踪脚本

    java - 使用文本文件中的数据创建重定向链接

    linux - 在 Cygwin 上使用 MPI 编译器链接错误?

    ssl - Cnames 和 ssl - 如果两个名称都有 ssl 好吗?

    android - 如何在 Android Lollipop 上更改 3g/4g DNS

    php - 我可以使用 Zend Framework 将对象添加到数据库的当前方式是什么?

    c++ - 确保在 C++ 中调用析构函数的信号处理

    python - 有没有办法让anaconda变小?

    javascript - 如何为 node.js 服务器分配域名?