PHPMailer无法发送电子邮件,客户端关闭连接


PHPMailer cannot send email, client closes connection?

嗯,我正试图从我的主机(hosting2go)使用smtp服务器发送电子邮件,根据他们的一个常见问题解答,我必须首先使用pop3进行身份验证,它必须检索邮件,然后我才能尝试连接到smtp服务器,这就是我所做的(我认为),但不幸的是,由于某种原因,它仍然断开了我与smtp服务器的连接,更令人困惑的是(如果我做对了)它告诉我客户端请求关闭连接。

这是我的php代码:

 echo 'running';
 require '../PHPMailerAutoload.php';
 $pop3mail = imap_open('{XXXX.hosting2go.nl:110/pop3}', 'noreply@wtvruinerwoldnieuw.nl', 'XXX');
// grab a list of all the mail headers
$headers = imap_headers($pop3mail);
// grab a header object for the last message in the mailbox
$last = imap_num_msg($pop3mail);
$header = imap_header($pop3mail, $last);
// grab the body for the same message
echo $body = imap_body($pop3mail, $last);

$mail = new PHPMailer;
$mail->SMTPDebug = 3; 
$mail->Debugoutput = 'html';                              // Enable verbose debug output
$mail->isSMTP();                                      // Set mailer to use SMTP 
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->SMTPSecure = 'tls';                            // zet SMTP naar ssl
$mail->Host = 'XXX.hosting2go.nl';  // Specify main and backup SMTP servers
$mail->Port = 25;                                    // TCP port to connect to
$mail->Username = "noreply@wtvruinerwoldnieuw.nl";                 // SMTP username
$mail->Password = 'XXXX';                           // SMTP password                          

$mail->From = 'noreply@wtvruinerwoldnieuw.nl';
$mail->FromName = 'Werktuigenvereniging Ruinerwold';

$mail->AddAddress("e.zenderink@gmail.com");

//$mail->isHTML(true);                                  // Set email format to HTML
$mail->Subject = "testing";
$mail->Body    = "testing";
//send the message, check for errors
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}
imap_close($pop3mail);

这就是我得到的回应:

runningConnection: opening to xxx.hosting2go.nl:25, t=300, opt=array ()
Connection: opened
SERVER -> CLIENT: 220 xxx.hosting2go.nl ESMTP
CLIENT -> SERVER: EHLO wtvruinerwoldnieuw.nl
SERVER -> CLIENT: 250-xxx.hosting2go.nl250-STARTTLS250-PIPELINING250     8BITMIME
CLIENT -> SERVER: STARTTLS
SERVER -> CLIENT: 220 ready for tls
CLIENT -> SERVER: QUIT

Connection: closed
SMTP connect() failed.
Mailer Error: SMTP connect() failed.

客户->服务器:QUIT让我很困惑,但我可能理解不正确。奇怪的是,php中构建的默认mail()函数运行得很好。但我不想使用这个功能,因为它将用于时事通讯等(不是最重要的部分)。这个函数和我在这里做的有什么不同?

更新:

是的,我正在查看PHPMailer和服务器的响应,偶然发现了关于STARTTLS的文档,并发现了这一点:

 If the SMTP client decides that the level of authentication or
 privacy is not high enough for it to continue, it SHOULD issue an
 SMTP QUIT command immediately after the TLS negotiation is complete.

来源:https://www.ietf.org/rfc/rfc2487.txt

但现在是服务器还是客户端的问题。我还尝试使用localhost(因为我尝试使用的smtp服务器与我的网站所在的服务器相同)(127.0.0.1),结果相同。

更新2#(找到了一个解决方案,但很奇怪):https://stackoverflow.com/a/12410579/4564466

评论:

$mail->isSMTP();

有效,我不知道为什么,对这个解决方案的回应并没有告诉我为什么有效。我不确定这是否是正确的方式,或者我现在正在做一些任何人都不应该做的事情…

答复如下:

runningServer -> Client: +OK Hello there. Server -> Client: +OK Password required. Server -> Client: +OK logged in. Message sent!

我收到的邮件非常好。

谢谢你的帮助。

附言:我不介意你试着给我指一个方向,而不是帮我把所有的东西都打出来,事实上,如果你给我指个方向,我会更喜欢,因为我确实想知道它是如何工作的,为什么不工作。

您所描述的内容在SMTP之前称为POP。我已经20多年没见过有人用它了!

不要麻烦滚动自己的代码-PHPMailer内置了对它的支持。看看这个代码示例。

也就是说,我建议找一家石器时代稍短的ISP。

您的服务器正试图使用STARTTLS将您置于TLS模式。您必须配置PHPMailer才能启用此模式。我认为你需要的是:

$mail->SMTPSecure = 'tls';