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

使用PhpMailer对接Office365发送邮件认证失败求助

Office365 + PhpMailer 发送邮件失败问题排查与解决方案

问题概述

使用PhpMailer对接Office365发送邮件时,反复出现两类错误:

  • 日志提示「用户凭据不正确」
  • 提示「用户被组织安全默认策略锁定」

已执行的操作:

  • 在Office后台启用SMTP并生成应用密码,使用应用密码验证
  • 无法关闭全局安全默认策略
  • 尝试过LOGIN/PLAIN/CRAM-MD5等AuthType,以及tls/ssl/STARTSSL等SMTPSecure组合,均未解决问题

相关代码

function sendClientMail($submitValue, $mail)
{
    $product = $submitValue['product'];

    try {
        $mail->SMTPDebug = 4;
        $mail->isSMTP();
        $mail->AuthType   = 'LOGIN';
        $mail->Host       = 'smtp.office365.com';
        $mail->SMTPAuth   = true;
        $mail->Username   = '';
        $mail->Password   = '';
        $mail->SMTPSecure = 'tls';
        $mail->Port       = 587;

        $mail->setFrom('', '');
        $mail->addAddress('', '');

        $mail->isHTML(true);
        $mail->Subject = "Welcome to " . returnApp($product);
        $mail->Body    = returnHTMLMailBody($product);
        $mail->AltBody = returnPlainTextMailBody($product);

        $mail->send();

        header("location:index.php?ok=" . $product);
    } catch (Exception $e) {
        echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
    }
}

解决方案

一、修正核心配置参数

Office365的SMTP有严格的配置要求,需调整以下参数:

  1. 移除手动指定的AuthType:Office365仅支持LOGIN和PLAIN认证,让PhpMailer自动协商即可,无需手动指定,避免兼容性问题
  2. 固定端口与加密方式:必须使用Port=587 + SMTPSecure='tls',ssl+465端口已被Office365逐步淘汰
  3. 发件人与认证邮箱一致:setFrom的邮箱地址必须和Username完全相同,Office365不允许跨邮箱发件

二、解决「用户被安全策略锁定」问题

若无法关闭全局安全默认策略,需单独为该邮箱启用SMTP权限:

  • Azure AD后台操作:登录Azure AD管理中心,找到目标用户→进入「邮件」设置→启用「SMTP AUTH」
  • PowerShell命令(若后台无对应选项):
    Set-CASMailbox -Identity "user@yourdomain.com" -SmtpClientAuthenticationDisabled $false
    
    注:需使用Exchange Online PowerShell模块执行

三、解决「凭据不正确」问题

  1. 确认用户名格式:Username必须是完整的Office365邮箱地址(如user@yourdomain.onmicrosoft.com或自定义域名邮箱)
  2. 验证应用密码有效性:应用密码需在用户Microsoft账户的「安全设置」中生成,生成后需立即复制保存,无法二次查看;确保使用的是该账户对应的应用密码,而非普通登录密码
  3. 检查MFA状态:应用密码仅适用于开启MFA的账户,若账户未开启MFA,直接使用普通密码即可(但不推荐,建议开启MFA后使用应用密码)

修正后的代码示例

function sendClientMail($submitValue, $mail)
{
    $product = $submitValue['product'];

    try {
        $mail->SMTPDebug = 4;
        $mail->isSMTP();
        $mail->Host       = 'smtp.office365.com';
        $mail->SMTPAuth   = true;
        $mail->Username   = 'your-full-email@domain.com'; // 替换为完整邮箱地址
        $mail->Password   = 'your-generated-app-password'; // 替换为应用密码
        $mail->SMTPSecure = 'tls';
        $mail->Port       = 587;

        // 发件人必须与认证邮箱一致
        $mail->setFrom('your-full-email@domain.com', 'Your Display Name');
        $mail->addAddress('recipient@example.com', 'Recipient Name'); // 替换为收件人信息

        $mail->isHTML(true);
        $mail->Subject = "Welcome to " . returnApp($product);
        $mail->Body    = returnHTMLMailBody($product);
        $mail->AltBody = returnPlainTextMailBody($product);

        $mail->send();

        header("location:index.php?ok=" . $product);
        exit; // 发送header后需终止后续代码执行
    } catch (Exception $e) {
        echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
    }
}

额外排查点

  • 检查服务器防火墙是否允许出站连接到587端口
  • 确保使用最新版本的PhpMailer,旧版本可能存在Office365兼容性问题
  • 若仍提示锁定,联系租户管理员确认是否存在条件访问策略限制SMTP的使用

内容的提问来源于stack exchange,提问作者daydr3am3r

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 13:02:04