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

使用Mailtrap时出现SMTP认证错误的原因及解决求助

解决Mailtrap + PHPMailer SMTP认证失败(535 Invalid credentials)问题

从调试日志能明确看到核心错误是535 5.7.0 Invalid credentials,结合你的代码和环境,按以下步骤排查解决:

  • 核对Mailtrap的SMTP凭证
    登录Mailtrap后台,打开对应的测试收件箱,复制页面显示的完整Username和Password——不要手动输入、部分隐藏或复制错误。Mailtrap每个测试收件箱都有独立的SMTP凭证,别混用其他收件箱的信息。

  • 强制指定兼容的认证方式
    调试日志显示服务器支持PLAIN、LOGIN、CRAM-MD5,但PHPMailer自动选择的CRAM-MD5认证失败。直接在代码里指定用PLAIN或LOGIN认证,在$phpmailer->Password配置后添加:

    $phpmailer->AuthType = 'PLAIN'; // 也可替换为'LOGIN'
    
  • 启用TLS加密(推荐)
    虽然2525端口支持无加密连接,但启用TLS能避免凭证传输风险,还能解决部分认证兼容性问题,添加配置:

    $phpmailer->SMTPSecure = 'tls';
    
  • 确认PHP扩展状态
    检查php.ini中extension=openssl是否被注释,取消注释后重启XAMPP的Apache服务,确保PHPMailer能正常使用加密功能。

修改后的完整代码示例

<?php
$phpmailer = new PHPMailer(true);
try {
    $phpmailer->isSMTP();
    $phpmailer->Host = 'sandbox.smtp.mailtrap.io';
    $phpmailer->SMTPAuth = true;
    $phpmailer->Port = 2525;
    $phpmailer->Username = '你的完整Mailtrap用户名';
    $phpmailer->Password = '你的完整Mailtrap密码';
    $phpmailer->AuthType = 'PLAIN';
    $phpmailer->SMTPSecure = 'tls';

    $phpmailer->setFrom('info@mailtrap.io', 'Mailtrap');
    $phpmailer->addReplyTo('info@mailtrap.io', 'Mailtrap');
    $phpmailer->addAddress('info@mailtrap.io');
    
    $phpmailer->Subject = 'Test Email via Mailtrap SMTP using PHPMailer';
    $phpmailer->isHTML(true);
    $mailContent = "<h1>Send HTML Email using SMTP in PHP</h1>
        <p>This is a test email I’m sending using SMTP mail server with PHPMailer.</p>";
    $phpmailer->Body = $mailContent;
    
    $phpmailer->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $phpmailer->ErrorInfo;
}

内容的提问来源于stack exchange,提问作者Sándor Kovács

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 18:57:21