【问题标题】:PHPMailer smtp connect missing 220PHPMailer smtp 连接丢失 220
【发布时间】:2017-03-30 14:33:19
【问题描述】:

我通过 PHPMailer 连接到 SMTP 服务器时遇到问题。

<?php
$mail = new PHPMailer();
$mail->isSMTP();

$mail->SMTPDebug = true;

$mail->Host = "ip.ip.ip.ip";
$mail->Port = "25";
$mail->Helo = "";
$mail->SMTPAutoTLS = false;
$mail->SMTPSecure = "";
$mail->SMTPAuth = false;
$mail->Timeout = 10;

$mail->From = "sender@example.com";
$mail->FromName = "Sender Name";

$mail->addAddress("receiver@example.com");

$mail->isHTML(true);
$mail->Subject = "Test";
$mail->Body = "<h1>Test</h1>";


if(!$mail->Send()) {
    echo $mail->ErrorInfo;
} else {
    echo "Mail sent...";
}

我发现了主要问题: 成功连接后,服务器不会直接发送“220 公告”。它除了等待 EHLO 什么都没有发送。在任何客户端输入后,服务器都会发送“220 公告”!我用 telnet 测试了一下。

我以前看到过它,我不知道它是否尊重 rfc。

EHLO 之后的 220 使 PHPMailer 完全崩溃。任何以下通信都是“误解”。我看到PHPMailer连接后正在等待220公告。但这失败了,因为服务器没有发送任何东西。当达到超时时,客户端发送一个 EHLO。之后它会收到 220 但以下通信失败。

这是完整的通信输出:

2017-03-30 13:38:48     Connection: opening to ip.ip.ip.ip:587, timeout=10, options=array (
                                          )
2017-03-30 13:38:48     Connection: opened
2017-03-30 13:38:58     SMTP -> get_lines(): $data is ""
2017-03-30 13:38:58     SMTP -> get_lines(): $str is  ""
2017-03-30 13:38:58     SMTP -> get_lines(): timed-out (10 sec)
2017-03-30 13:38:58     SERVER -> CLIENT:
2017-03-30 13:38:58     CLIENT -> SERVER: EHLO conweb1.example.de
2017-03-30 13:38:58     SMTP -> get_lines(): $data is ""
2017-03-30 13:38:58     SMTP -> get_lines(): $str is  "220 EX-02.example.loc Microsoft ESMTP MAIL Service ready at Thu, 30 Mar 2017 15:38:58 +0200
                                          "
2017-03-30 13:38:58     SERVER -> CLIENT: 220 EX-02.example.loc Microsoft ESMTP MAIL Service ready at Thu, 30 Mar 2017 15:38:58 +0200
2017-03-30 13:38:58     SMTP ERROR: EHLO command failed: 220 EX-02.example.loc Microsoft ESMTP MAIL Service ready at Thu, 30 Mar 2017 15:38:58 +0200
2017-03-30 13:38:58     CLIENT -> SERVER: HELO conweb1.example.de
2017-03-30 13:38:58     SMTP -> get_lines(): $data is ""
2017-03-30 13:38:58     SMTP -> get_lines(): $str is  "250-EX-02.example.loc Hello [ip.ip.ip.ip]
                                          "
2017-03-30 13:38:58     SMTP -> get_lines(): $data is "250-EX-02.example.loc Hello [ip.ip.ip.ip]
                                          "
2017-03-30 13:38:58     SMTP -> get_lines(): $str is  "250-SIZE 36700160
                                          "
2017-03-30 13:38:58     SMTP -> get_lines(): $data is "250-EX-02.example.loc Hello [ip.ip.ip.ip]
                                          250-SIZE 36700160
                                          "
2017-03-30 13:38:58     SMTP -> get_lines(): $str is  "250-PIPELINING
                                          "
2017-03-30 13:38:58     SMTP -> get_lines(): $data is "250-EX-02.example.loc Hello [ip.ip.ip.ip]
                                          250-SIZE 36700160
                                          250-PIPELINING
                                          "
2017-03-30 13:38:58     SMTP -> get_lines(): $str is  "250-DSN
                                          "
2017-03-30 13:38:58     SMTP -> get_lines(): $data is "250-EX-02.example.loc Hello [ip.ip.ip.ip]
                                          250-SIZE 36700160
                                          250-PIPELINING
                                          250-DSN
                                          "
2017-03-30 13:38:58     SMTP -> get_lines(): $str is  "250-ENHANCEDSTATUSCODES
                                          "
2017-03-30 13:38:58     SMTP -> get_lines(): $data is "250-EX-02.example.loc Hello [ip.ip.ip.ip]
                                          250-SIZE 36700160
                                          250-PIPELINING
                                          250-DSN
                                          250-ENHANCEDSTATUSCODES
                                          "
2017-03-30 13:38:58     SMTP -> get_lines(): $str is  "250-STARTTLS
                                          "
2017-03-30 13:38:58     SMTP -> get_lines(): $data is "250-EX-02.example.loc Hello [ip.ip.ip.ip]
                                          250-SIZE 36700160
                                          250-PIPELINING
                                          250-DSN
                                          250-ENHANCEDSTATUSCODES
                                          250-STARTTLS
                                          "
2017-03-30 13:38:58     SMTP -> get_lines(): $str is  "250-AUTH LOGIN
                                          "
2017-03-30 13:38:58     SMTP -> get_lines(): $data is "250-EX-02.example.loc Hello [ip.ip.ip.ip]
                                          250-SIZE 36700160
                                          250-PIPELINING
                                          250-DSN
                                          250-ENHANCEDSTATUSCODES
                                          250-STARTTLS
                                          250-AUTH LOGIN
                                          "
2017-03-30 13:38:58     SMTP -> get_lines(): $str is  "250-8BITMIME
                                          "
2017-03-30 13:38:58     SMTP -> get_lines(): $data is "250-EX-02.example.loc Hello [ip.ip.ip.ip]
                                          250-SIZE 36700160
                                          250-PIPELINING
                                          250-DSN
                                          250-ENHANCEDSTATUSCODES
                                          250-STARTTLS
                                          250-AUTH LOGIN
                                          250-8BITMIME
                                          "
2017-03-30 13:38:58     SMTP -> get_lines(): $str is  "250-BINARYMIME
                                          "
2017-03-30 13:38:58     SMTP -> get_lines(): $data is "250-EX-02.example.loc Hello [ip.ip.ip.ip]
                                          250-SIZE 36700160
                                          250-PIPELINING
                                          250-DSN
                                          250-ENHANCEDSTATUSCODES
                                          250-STARTTLS
                                          250-AUTH LOGIN
                                          250-8BITMIME
                                          250-BINARYMIME
                                          "
2017-03-30 13:38:58     SMTP -> get_lines(): $str is  "250 CHUNKING
                                          "
2017-03-30 13:38:58     SERVER -> CLIENT: 250-EX-02.example.loc Hello [ip.ip.ip.ip]
                                          250-SIZE 36700160
                                          250-PIPELINING
                                          250-DSN
                                          250-ENHANCEDSTATUSCODES
                                          250-STARTTLS
                                          250-AUTH LOGIN
                                          250-8BITMIME
                                          250-BINARYMIME
                                          250 CHUNKING
2017-03-30 13:38:58     CLIENT -> SERVER: MAIL FROM:<personal@example.com>
2017-03-30 13:39:03     SMTP -> get_lines(): $data is ""
2017-03-30 13:39:03     SMTP -> get_lines(): $str is  "250 EX-02.example.loc Hello [ip.ip.ip.ip]
                                          "
2017-03-30 13:39:03     SERVER -> CLIENT: 250 EX-02.example.loc Hello [ip.ip.ip.ip]
2017-03-30 13:39:03     CLIENT -> SERVER: RCPT TO:<tm@example.com>
2017-03-30 13:39:03     SMTP -> get_lines(): $data is ""
2017-03-30 13:39:03     SMTP -> get_lines(): $str is  "250 2.1.0 Sender OK
                                          "
2017-03-30 13:39:03     SERVER -> CLIENT: 250 2.1.0 Sender OK
2017-03-30 13:39:03     CLIENT -> SERVER: DATA
2017-03-30 13:39:03     SMTP -> get_lines(): $data is ""
2017-03-30 13:39:03     SMTP -> get_lines(): $str is  "250 2.1.5 Recipient OK
                                          "
2017-03-30 13:39:03     SERVER -> CLIENT: 250 2.1.5 Recipient OK
2017-03-30 13:39:03     SMTP ERROR: DATA command failed: 250 2.1.5 Recipient OK
2017-03-30 13:39:03     SMTP Error: data not accepted.
SMTP Error: data not accepted.SMTP server error: DATA command failed Detail: Recipient OK
 SMTP code: 250 Additional SMTP info: 2.1.52017-03-30 13:39:03  CLIENT -> SERVER: QUIT
2017-03-30 13:39:03     SMTP -> get_lines(): $data is ""
2017-03-30 13:39:03     SMTP -> get_lines(): $str is  "354 Start mail input; end with <CRLF>.<CRLF>
2017-03-30 13:39:03     SMTP ERROR: QUIT command failed: 354 Start mail input; end with <CRLF>.<CRLF>
2017-03-30 13:39:03     Connection: closed

主要问题是: 错误是由PHP-Mailer的错误协议实现引起的吗? 还是 SMTP 服务器配置错误?

【问题讨论】:

  • 您询问是否可以在 smtp 服务器上执行命令,您可以使用 telnet 测试以验证是否可以执行命令,另一个观察结果似乎数据为空
  • 尝试在 $mail->Body 之后添加 $mail->AltBody ="Test"
  • @GuillermoAndresFuentesMoral 数据不是空的!该代码适用于其他 smtp 服务器!我成功地测试了telnet。原因是响应延迟是由开始时的220后期造成的。客户会误解任何以下响应。如您所见,首先由于缺少 220 问候而导致超时。
  • 他们可以将服务器配置为在发送问候语之前延迟超过 10 秒(以减少垃圾邮件)。在收到问候语之前发送 helo/ehlo 是违反 rfc 的,但您可能需要等待更长时间。通过 telnet,如果您连接并让它静置,它最终会发送问候语吗?
  • @drew010 不!不是在我发送任何东西之前。但是如果我发送一些东西,它会立即回复 220...

标签: php smtp phpmailer smtpclient


【解决方案1】:

不要这样做:

$mail->Timeout = 10;

RFC2821 指定初始问候语的超时时间为 300 秒(5 分钟),并且一些服务器在此施加较长的延迟(称为“问候延迟”),专门用于捕获不符合要求的客户端,通常是垃圾邮件程序。 PHPMailer 完全尊重这一点,它的默认 Timeout 设置为 300 秒。如果你缩短它,你会阻止它等待足够长的时间。如果将其设置为 300 或保留其默认设置,它是否有效?

如果您说“但我不想在页面处理过程中等待那么久!” - 欢迎来到 SMTP 的世界 - 它不是为实时使用而设计的;使用本地邮件服务器中继来消除这种延迟并让邮件服务器担心它。

【讨论】:

  • 好的。我将尝试等待 300 秒。但我怀疑这不是问题。我的 ip 在远程 smtp 上被列入白名单,其他人无法连​​接。因此,他们为垃圾邮件保护选择了一个问候延迟,这将是非常令人惊讶的。但也许这是微软的默认功能,他们没有改变它。我会尽力让你知道发生了什么。
猜你喜欢
  • 2014-06-13
  • 2015-08-09
  • 2016-11-10
  • 2014-05-20
  • 1970-01-01
  • 1970-01-01
  • 2015-09-25
  • 2016-07-14
  • 2021-01-30
相关资源
最近更新 更多