首页 > 解决方案 > phpmailer SMTP 错误“未接受数据”和“检查是否连接时捕获 EOF”

问题描述

我在 Windows 10 IIS 上运行的 PhpMailer 上使用 SMTP 发送超过 9634 字节的文本文件附件时遇到问题。PHP 版本是 8.0.3 我连接/验证到远程 SMTP 服务器没有问题。如果没有附件,我可以发送电子邮件。我可以发送带有附件的电子邮件,其文件大小在 1 到 9634 字节之间。但是,当我尝试附加大于 9634 字节的文件时,我收到 SMTP 错误“数据未接受”。使用我的 Exchange Online 凭据在另一台远程服务器 smtp.office365.com 上发生同样的事情。完全相同的文本文件大小。有任何想法吗?

测试phpmailer.php

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;

require 'vendor/autoload.php';
$mail = new PHPMailer(true);
$mail->CharSet = 'UTF-8';
$mail->isSMTP();
$mail->SMTPDebug =  3;                                  
$mail->Debugoutput = 'error_log';
$mail->Host = 'smtp.reg365.net';            
$mail->SMTPAuth = true;                                 
$mail->Username = 'redacted';            
$mail->Password = 'redacted';                         
$mail->SMTPSecure = 'tls';                              
$mail->Port = 587;                                      

$FromName = "Joe in joemurray.eu"; 
$mail->setFrom("joe@joemurray.eu", $FromName); 
$mail->isHTML(true);                               

$mail->Subject = "Test 1 FRi from joemurray.eu";
$mail->Body    = "<i>This is the Body in Italics</i>";
$mail->AltBody = "Please ignore";
$mail->addAddress("amazon.murray754@gmail.com","Amazon Joe");

echo "<br /><h2>STARTING to Add Attachment:<br /></h2>";
try {
    $filename = 'testdata/text.txt';
    $mail->addAttachment($filename);
    echo $filename . ': ' . filesize($filename) . ' bytes';
}        
catch (Exception $e) {
    echo $e->getMessage()." exiting....";
    exit();
}
finally {
    echo "<br /><h2>FINISHED addAttachment OK</h2>";
}

try { //presend and save MIME message
    $mail->preSend();
    $message = $mail->getSentMIMEMessage();
    file_put_contents('MIMEOUT.txt',$message);
    }
catch (exeption $e2) {
    echo "ERROR: ".$ex->getMessage()." exiting....<br />";
    exit();
}



try {
    $mail->send();
} 
catch (Exception $ex) {
    echo "ERROR: ".$ex->getMessage()." exiting....<br />";
    exit();
}
finally {
    echo "<br /><h2>Sent OK</h2>";
}
?>

错误日志调试输出:

[24-Apr-2021 17:00:08 UTC] Connection: opening to smtp.reg365.net:587, timeout=300, options=array()
[24-Apr-2021 17:00:08 UTC] Connection: opened
[24-Apr-2021 17:00:09 UTC] SERVER -> CLIENT: 220 smtp.reg365.net ESMTP

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: EHLO localhost

[24-Apr-2021 17:00:09 UTC] SERVER -> CLIENT: 250-smtp.reg365.net Hello localhost [83.70.118.68]
250-SIZE 157286400
250-8BITMIME
250-PIPELINING
250-X_PIPE_CONNECT
250-AUTH LOGIN PLAIN
250-STARTTLS
250 HELP

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: STARTTLS

[24-Apr-2021 17:00:09 UTC] SERVER -> CLIENT: 220 TLS go ahead

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: EHLO localhost

[24-Apr-2021 17:00:09 UTC] SERVER -> CLIENT: 250-smtp.reg365.net Hello localhost [redacted]
250-SIZE 157286400
250-8BITMIME
250-PIPELINING
250-X_PIPE_CONNECT
250-AUTH LOGIN PLAIN
250 HELP

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: AUTH LOGIN

[24-Apr-2021 17:00:09 UTC] SERVER -> CLIENT: 334 VXNlcm5hbWU6

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: [credentials hidden]
[24-Apr-2021 17:00:09 UTC] SERVER -> CLIENT: 334 UGFzc3dvcmQ6

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: [credentials hidden]
[24-Apr-2021 17:00:09 UTC] SERVER -> CLIENT: 235 Authentication succeeded

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: MAIL FROM:<joe@joemurray.eu>

[24-Apr-2021 17:00:09 UTC] SERVER -> CLIENT: 250 OK

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: RCPT TO:<amazon.murray754@gmail.com>

[24-Apr-2021 17:00:09 UTC] SERVER -> CLIENT: 250 Accepted

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: DATA

[24-Apr-2021 17:00:09 UTC] SERVER -> CLIENT: 354 Enter message, ending with "." on a line by itself

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: Date: Sat, 24 Apr 2021 17:00:08 +0000

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: To: Amazon Joe <amazon.murray754@gmail.com>

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: From: "Joe in joemurray.eu" <joe@joemurray.eu>

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: Subject: Test 1 FRi from joemurray.eu

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: Message-ID: <0F8QgKoPS4rTUObfvGkgWZvwXjAKpP8cXGDm3Vsb8@localhost>

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: X-Mailer: PHPMailer 6.4.0 (https://github.com/PHPMailer/PHPMailer)

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: MIME-Version: 1.0

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: Content-Type: multipart/mixed;

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER:  boundary="b1_0F8QgKoPS4rTUObfvGkgWZvwXjAKpP8cXGDm3Vsb8"

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: Content-Transfer-Encoding: 8bit

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: 

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: This is a multi-part message in MIME format.

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: 

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: --b1_0F8QgKoPS4rTUObfvGkgWZvwXjAKpP8cXGDm3Vsb8

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: Content-Type: multipart/alternative;

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER:  boundary="b2_0F8QgKoPS4rTUObfvGkgWZvwXjAKpP8cXGDm3Vsb8"

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: 

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: --b2_0F8QgKoPS4rTUObfvGkgWZvwXjAKpP8cXGDm3Vsb8

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: Content-Type: text/plain; charset=us-ascii

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: 

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: Please ignore

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: 

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: --b2_0F8QgKoPS4rTUObfvGkgWZvwXjAKpP8cXGDm3Vsb8

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: Content-Type: text/html; charset=us-ascii

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: 

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: <i>This is the Body in Italics</i>

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: 

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: 

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: --b2_0F8QgKoPS4rTUObfvGkgWZvwXjAKpP8cXGDm3Vsb8--

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: 

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: --b1_0F8QgKoPS4rTUObfvGkgWZvwXjAKpP8cXGDm3Vsb8

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: Content-Type: text/plain; name=text.txt

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: Content-Transfer-Encoding: base64

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: Content-Disposition: attachment; filename=text.txt

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: 

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: MDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAw

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: MDAwMDAwMDAwMDAwMDAwMDANCjAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAw

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: MDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwDQowMDAwMDAwMDAwMDAwMDAwMDAw

Lots more base64 lines like above

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: MDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAN

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: Cg==

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: 

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: --b1_0F8QgKoPS4rTUObfvGkgWZvwXjAKpP8cXGDm3Vsb8--

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: 

[24-Apr-2021 17:00:09 UTC] CLIENT -> SERVER: .

[24-Apr-2021 17:00:09 UTC] SERVER -> CLIENT: 
[24-Apr-2021 17:00:09 UTC] SMTP ERROR: DATA END command failed: 
[24-Apr-2021 17:00:09 UTC] SMTP Error: data not accepted.
[24-Apr-2021 17:00:09 UTC] SMTP NOTICE: EOF caught while checking if connected
[24-Apr-2021 17:00:09 UTC] Connection: closed

浏览器窗口中的输出是:

STARTING to Add Attachment:
testdata/text.txt: 9634 bytes
FINISHED addAttachment OK
ERROR: SMTP Error: data not accepted. exiting....

test.txt 文件由多行零组成,每行 76 个字节长,包括行尾的 Windows CRLF。

标签: phpmailer

解决方案


推荐阅读