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

PHP Web应用Outlook邮件发送认证失败及免费解决方案咨询

解决普通Outlook账号SMTP基础认证禁用问题的免费方案

针对你用PHPMailer搭配普通Outlook账号发送邮件失败的问题(因基础认证被禁用),这里提供几个免费且长期可行的解决方案:

方案1:给PHPMailer配置OAuth2认证

PHPMailer其实支持OAuth2认证,只是默认未启用。你需要先在Microsoft Azure门户注册一个免费应用(用于获取OAuth2凭证),再调整代码:

操作步骤:

  1. 注册Azure应用:
    • 登录Azure门户,创建「单页应用/移动应用」类型的应用
    • 配置重定向URI(本地测试可填http://localhost)
    • 获取客户端ID、客户端密钥,以及授权范围https://outlook.office.com/SMTP.Send
  2. 获取访问令牌:
    • 通过OAuth2授权码流程获取access token,可编写临时脚本获取,或集成到应用登录流程
  3. 修改PHPMailer代码:
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;
    use PHPMailer\PHPMailer\OAuth;
    
    require("vendor/autoload.php");
    
    function sendmail()
    {
        $mail = new PHPMailer(true);
    
        try {
            $mail->isSMTP();
            $mail->Host = 'smtp-mail.outlook.com';
            $mail->SMTPAuth = true;
            $mail->AuthType = 'XOAUTH2'; // 关键:启用OAuth2认证
            $mail->Username = '你的Outlook邮箱';
            $mail->Port = 587;
            $mail->SMTPSecure = 'tls';
            $mail->SMTPDebug = 2;
    
            // 配置OAuth2参数
            $mail->setOAuth(
                new OAuth(
                    [
                        'clientId' => '你的Azure应用客户端ID',
                        'clientSecret' => '你的Azure应用客户端密钥',
                        'refreshToken' => '获取到的刷新令牌',
                        'userName' => '你的Outlook邮箱',
                        'tokenProvider' => function ($oauth) {
                            // 可实现自动刷新token逻辑,或提前传入有效token
                            $oauth->setAccessToken('你的访问令牌');
                            return $oauth;
                        }
                    ]
                )
            );
    
            $mail->setFrom('你的Outlook邮箱', '你的名称');
            $mail->addAddress('收件人邮箱');
            $mail->isHTML(true);
    
            $mail->Subject = 'Test Email';
            $mail->Body    = '<h1>Welcome to Our Service</h1><p>Enjoy your experience!</p>';
            
            $mail->send();
            echo "Message sent successfully!";
        } catch (Exception $e) {
            echo "Message could not be sent. Error: {$mail->ErrorInfo}";
        }
    }
    
    注意:刷新令牌可长期使用,到期前可通过客户端密钥自动刷新,无需手动干预。

方案2:改用支持OAuth2的邮件库——Symfony Mailer

Symfony Mailer是当前维护活跃的邮件库,对OAuth2支持更友好,配置更简洁:

  1. 安装依赖:
    composer require symfony/mailer symfony/microsoft-mailer
    
  2. 配置OAuth2并发送邮件:
    use Symfony\Component\Mailer\Mailer;
    use Symfony\Component\Mailer\Transport;
    use Symfony\Component\Mime\Email;
    
    require 'vendor/autoload.php';
    
    // 配置OAuth2传输链接
    $dsn = 'smtp+oauth2://{client_id}:{client_secret}@smtp-mail.outlook.com:587?auth_mode=oauth2&user={你的Outlook邮箱}&refresh_token={你的刷新令牌}';
    $transport = Transport::fromDsn($dsn);
    $mailer = new Mailer($transport);
    
    $email = (new Email())
        ->from('你的Outlook邮箱')
        ->to('收件人邮箱')
        ->subject('Test Email')
        ->html('<h1>Welcome to Our Service</h1><p>Enjoy your experience!</p>');
    
    $mailer->send($email);
    echo "邮件发送成功!";
    

方案3:使用免费第三方邮件服务(最省心)

如果不想折腾OAuth2配置,推荐用免费第三方邮件服务,免费额度足够副业项目使用:

  • SendGrid:每月10000封免费邮件
  • Mailgun:每月5000封免费邮件
  • Postmark:每月10000封免费测试邮件,正式使用也有免费层

以SendGrid为例,配置PHPMailer的代码:

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require("vendor/autoload.php");

function sendmail()
{
    $mail = new PHPMailer(true);

    try {
        $mail->isSMTP();
        $mail->Host = 'smtp.sendgrid.net';
        $mail->SMTPAuth = true;
        $mail->Username = 'apikey'; // 固定值
        $mail->Password = '你的SendGrid API密钥';
        $mail->SMTPSecure = 'tls';
        $mail->Port = 587;
        $mail->SMTPDebug = 2;

        $mail->setFrom('你的发件邮箱(需在SendGrid验证)', '你的名称');
        $mail->addAddress('收件人邮箱');
        $mail->isHTML(true);

        $mail->Subject = 'Test Email';
        $mail->Body    = '<h1>Welcome to Our Service</h1><p>Enjoy your experience!</p>';
        
        $mail->send();
        echo "Message sent successfully!";
    } catch (Exception $e) {
        echo "Message could not be sent. Error: {$mail->ErrorInfo}";
    }
}

这类服务的免费层长期有效,无需Microsoft 365账号,配置简单,还能降低邮件被标记为垃圾邮件的概率。


内容的提问来源于stack exchange,提问作者Thomas B.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 21:29:51