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

Ubuntu Apache服务器上CodeIgniter无法发送SMTP邮件问题

排查Ubuntu Apache环境下CodeIgniter通过Office365 SMTP发件失败问题

报错信息:

Failed to authenticate password. Error:
Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.

针对该问题,按以下步骤逐一排查:

  • 检查服务器网络与端口连通性
    在Ubuntu服务器终端执行命令,测试能否连接Office365的SMTP端口:

    nc -zv smtp.office365.com 587
    # 或使用telnet测试
    telnet smtp.office365.com 587
    

    如果连接失败,说明服务器防火墙(如ufw)、机房防火墙或云服务器安全组封禁了587端口,需要开放对应端口权限。

  • 验证PHP OpenSSL扩展状态
    Office365的TLS加密依赖OpenSSL扩展,执行命令检查是否启用:

    php -m | grep openssl
    

    若未找到openssl条目,编辑对应PHP版本的apache2配置文件(例如/etc/php/8.1/apache2/php.ini),找到;extension=openssl并删除前面的分号,随后重启Apache服务:

    sudo systemctl restart apache2
    
  • 核对App密码与账号配置

    • 确保$config['smtp_user']是完整的Office365邮箱地址(如xxx@outlook.com),不能仅填写用户名
    • 确认使用的是应用专用密码(开启MFA的账号必须使用App密码,不能用原账号密码),检查密码是否输入正确,无多余空格或转义错误
  • 确认换行符配置正确性
    Office365 SMTP要求必须使用\r\n作为换行符,检查配置是否符合要求:

    $config['newline'] = "\r\n"; // 必须用双引号,单引号会导致转义失效
    
  • 查看详细SMTP调试日志
    完整打印print_debugger()的输出,里面包含SMTP交互的详细过程,可定位认证失败的具体原因:

    echo $this->email->print_debugger();
    // 也可输出到日志文件避免页面显示
    file_put_contents('/var/log/ci_email_debug.log', $this->email->print_debugger(), FILE_APPEND);
    
  • 同步服务器系统时间
    TLS认证对时间同步要求严格,若服务器时间与实际时间偏差超过5分钟,会导致证书验证失败。执行以下命令开启NTP时间同步:

    sudo timedatectl set-ntp on
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 14:15:29