我正在尝试构建一个可以检查微软邮件登录是否正常工作的 php 脚本,我使用 PHPMailer 类https://github.com/PHPMailer/PHPMailer
这是我的代码
date_default_timezone_set('Etc/UTC');
require '../PHPMailerAutoload.php';
//Create a new SMTP instance
$smtp = new SMTP;
//Enable connection-level debug output
$smtp->do_debug = SMTP::DEBUG_CONNECTION;
try {
//Connect to an SMTP server
if ($smtp->connect('smtp.office365.com', 587)) {
//Say hello
if ($smtp->hello('localhost')) { //Put your host name in here
//Authenticate
if ($smtp->authenticate('test1@sjnewman.co.uk', 'Kara9802')) {
echo "Connected ok!";
} else {
throw new Exception('Authentication failed: ' . $smtp->getLastReply());
}
} else {
throw new Exception('HELO failed: '. $smtp->getLastReply());
}
} else {
throw new Exception('Connect failed');
}
} catch (Exception $e) {
echo 'SMTP error: '. $e->getMessage(), "'n";
}
//Whatever happened, close the connection.
$smtp->quit(true);
脚本返回调试输出并显示身份验证失败:
2016-03-19 13:08:49 Connection: opening to smtp.office365.com:587, timeout=30, options=array ( ) 2016-03-19 13:08:49 Connection: opened 2016-03-19 13:08:49
SERVER -> CLIENT: 220 BLUPR19CA0034.outlook.office365.com Microsoft ESMTP MAIL Service ready at Sat, 19 Mar 2016 13:08:48 +0000 2016-03-19 13:08:49 CLIENT -> SERVER: EHLO localhost 2016-03-19 13:08:49
SERVER -> CLIENT: 250-BLUPR19CA0034.outlook.office365.com Hello [204.93.163.59] 250-SIZE 157286400 250-PIPELINING 250-DSN 250-ENHANCEDSTATUSCODES 250-STARTTLS 250-8BITMIME 250-BINARYMIME 250 CHUNKING SMTP error: Authentication failed: 250-BLUPR19CA0034.outlook.office365.com Hello [204.93.163.59] 250-SIZE 157286400 250-PIPELINING 250-DSN 250-ENHANCEDSTATUSCODES 250-STARTTLS 250-8BITMIME 250-BINARYMIME 250 CHUNKING 2016-03-19 13:08:49 CLIENT -> SERVER: QUIT 2016-03-19 13:08:49
SERVER -> CLIENT: 221 2.0.0 Service closing transmission channel 2016-03-19 13:08:49 Connection: closed
但是当我使用相同的登录名在 PHPMailer 上发送邮件时,我不确定为什么它不适用于上述脚本
任何人都知道如何解决这个问题。
非常简单:与大多数服务器一样,此服务器不支持未加密的身份验证。请注意,其响应中的功能列表不包括 AUTH
,但包括 STARTTLS
。因此,在尝试身份验证之前,您需要这样做(并重复您的 EHLO)。常规的PHPMailer会为你做这件事,所以它可以工作。如果你看看PHPMailer中smtpConnect
函数的源代码,你会看到它是如何做到的,你需要从中挑选基础知识。