You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法通过PHPMailer连接Office365 SMTP服务器的求助

解决Office365 SMTP认证失败(535 5.7.3)的问题

从你提供的调试日志和PHP代码来看,核心问题是SMTP认证失败(返回535 5.7.3 Authentication unsuccessful),这是连接Office365 SMTP时非常常见的问题,我给你整理了几个针对性的排查和解决步骤:

一、基础信息校验

  • 确认邮箱账号和密码完全正确:你的代码里用的MAIL@DOMAIN.TLD和PASS要确保是实际可用的Office365邮箱凭据,注意密码如果包含特殊字符(比如!@#$),在PHP字符串里是否需要转义(比如用单引号包裹避免解析问题)。
  • 发件地址与认证账号一致:Office365默认要求setFrom的邮箱必须和SMTP认证的Username完全匹配,你的代码里已经符合这个要求,但要确认该邮箱拥有发送邮件的权限。

二、针对MFA(多因素认证)的处理

如果你的Office365账号启用了多因素认证,普通密码无法用于SMTP客户端认证,必须使用应用密码:

  1. 登录Office365账户,进入安全设置页面
  2. 生成一个专门用于SMTP客户端的应用密码,替换代码中的PASS

三、租户与服务器环境配置检查

  • 检查Office365租户设置:有些管理员可能禁用了传统SMTP客户端认证,需要联系租户管理员确认是否允许SMTP AUTH协议。
  • 确保服务器支持TLS 1.2+:Office365已经不再支持TLS 1.0/1.1,你需要确认服务器的OpenSSL版本支持TLS 1.2。可以在代码中强制指定TLS版本,添加以下配置:
$mail->SMTPOptions = array(
    'ssl' => array(
        'verify_peer' => true,
        'verify_peer_name' => true,
        'crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT
    )
);

注意:临时测试可以把verify_peer设为false,但生产环境建议保持true,确保服务器CA证书配置正确。

四、PHPMailer版本更新

旧版本的PHPMailer可能存在与Office365现代认证兼容的问题,建议更新到最新稳定版,确保支持Office365的SMTP协议要求。


你的调试信息:

2018-05-16 08:12:52 Connection: opening to smtp.office365.com:587, timeout=300, options=array ( ) 2018-05-16 08:12:52 Connection: opened 2018-05-16 08:12:52 SERVER -> CLIENT: 220 CWLP265CA0229.outlook.office365.com Microsoft ESMTP MAIL Service ready at Wed, 16 May 2018 08:12:52 +0000 2018-05-16 08:12:52 CLIENT -> SERVER: EHLO localhost 2018-05-16 08:12:52 SERVER -> CLIENT: 250-CWLP265CA0229.outlook.office365.com Hello [109.81.243.32] 250-SIZE 157286400 250-PIPELINING 250-DSN 250-ENHANCEDSTATUSCODES 250-STARTTLS 250-8BITMIME 250-BINARYMIME 250-CHUNKING 250 SMTPUTF8 2018-05-16 08:12:52 CLIENT -> SERVER: STARTTLS 2018-05-16 08:12:52 SERVER -> CLIENT: 220 2.0.0 SMTP server ready 2018-05-16 08:12:52 CLIENT -> SERVER: EHLO localhost 2018-05-16 08:12:52 SERVER -> CLIENT: 250-CWLP265CA0229.outlook.office365.com Hello [109.81.243.32] 250-SIZE 157286400 250-PIPELINING 250-DSN 250-ENHANCEDSTATUSCODES 250-AUTH LOGIN XOAUTH2 250-8BITMIME 250-BINARYMIME 250-CHUNKING 250 SMTPUTF8 2018-05-16 08:12:52 CLIENT -> SERVER: AUTH LOGIN 2018-05-16 08:12:52 SERVER -> CLIENT: 334 VXNlcm5hbWU6 2018-05-16 08:12:52 CLIENT -> SERVER: xxx== 2018-05-16 08:12:52 SERVER -> CLIENT: 334 UGFzc3dvcmQ6 2018-05-16 08:12:52 CLIENT -> SERVER: xxx= 2018-05-16 08:12:58 SERVER -> CLIENT: 535 5.7.3 Authentication unsuccessful [CWLP265CA0229.GBRP265.PROD.OUTLOOK.COM] 2018-05-16 08:12:58 SMTP ERROR: Password command failed: 535 5.7.3 Authentication unsuccessful [CWLP265CA0229.GBRP265.PROD.OUTLOOK.COM] 2018-05-16 08:12:58 SMTP Error: Could not authenticate. 2018-05-16 08:12:58 CLIENT -> SERVER: QUIT 2018-05-16 08:12:58 SERVER -> CLIENT: 221 2.0.0 Service closing transmission channel 2018-05-16 08:12:58 Connection: closed 2018-05-16 08:12:58 SMTP connect() failed.

你的PHP代码:

$mail = new PHPMailer; 
$mail->SMTPDebug = 3; 
$mail->isSMTP(); 
$mail->Host = 'smtp.office365.com'; 
$mail->SMTPAuth = true; 
$mail->Username = 'MAIL@DOMAIN.TLD'; 
$mail->Password = 'PASS'; 
$mail->SMTPSecure = 'tls'; 
$mail->Port = 587; // TCP port to connect to 
$mail->setFrom('MAIL@DOMAIN.TLD'); 
$mail->addAddress('TOMAIL'); // Add a recipient 
$mail->isHTML(true); // Set email format to HTML 
$mail->Subject = 'Here is the subject'; 
$mail->Body = 'This is the HTML message body <b>in bold!</b>'; 

if(!$mail->send()) { 
    echo 'Message could not be sent.'; 
    echo 'Mailer Error: ' . $mail->ErrorInfo; 
} else { 
    echo 'Message has been sent'; 
}

内容的提问来源于stack exchange,提问作者Petr Kožnar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 04:06:36