【问题标题】:PHPMailer: appears not to be sending passwordPHPMailer:似乎没有发送密码
【发布时间】:2021-06-03 00:00:32
【问题描述】:

我检查了其他问题,但找不到与我的问题匹配的问题。我正在尝试使用 PHPMailer 向我的电子邮件提供商(Pixel Internet)发送一封电子邮件给自己,但验证失败。我使用与 Thunderbird 相同的主机名、用户名和密码,而且效果很好,所以我的代码中一定有错误。

这是我的代码:

        $mail->isSMTP();
        $mail->Host = $mail_smtp_host;
        $mail->Port = 587;
        $mail->SMTPAuth = true;
        $mail->AuthType = "PLAIN";
        $mail->SMTPSecure = 'tls';
        $mail->Username = $mail_auth_username;
        $mail->Password = $mail_auth_password; // (Password in plain text)
        $mail->SMTPDebug = 4;
        $mail->setFrom('(my email address)', 'Jeff Goodenough');
        $mail->addAddress(...);
        $mail->Subject = 'Test';
        $mail->Body = 'Testing.';
        $mail->send();

这是我得到的调试跟踪:

2021-03-04 10:29:43 Connection: opening to mail50.extendcp.co.uk:587, timeout=300, options=array()
2021-03-04 10:29:43 Connection: opened
2021-03-04 10:29:43 SMTP INBOUND: "220 mta1.hi.local ESMTP Exim 4.94 Thu, 04 Mar 2021 10:29:43 +0000"
2021-03-04 10:29:43 SERVER -> CLIENT: 220 mta1.hi.local ESMTP Exim 4.94 Thu, 04 Mar 2021 10:29:43 +0000
2021-03-04 10:29:43 CLIENT -> SERVER: EHLO localhost
2021-03-04 10:29:43 SMTP INBOUND: "250-mta1.hi.local Hello host86-151-185-147.range86-151.btcentralplus.com [86.151.185.147]"
2021-03-04 10:29:43 SMTP INBOUND: "250-SIZE 52428800"
2021-03-04 10:29:43 SMTP INBOUND: "250-8BITMIME"
2021-03-04 10:29:43 SMTP INBOUND: "250-PIPELINING"
2021-03-04 10:29:43 SMTP INBOUND: "250-X_PIPE_CONNECT"
2021-03-04 10:29:43 SMTP INBOUND: "250-AUTH PLAIN LOGIN CRAM-MD5"
2021-03-04 10:29:43 SMTP INBOUND: "250-CHUNKING"
2021-03-04 10:29:43 SMTP INBOUND: "250-STARTTLS"
2021-03-04 10:29:43 SMTP INBOUND: "250 HELP"
2021-03-04 10:29:43 SERVER -> CLIENT: 250-mta1.hi.local Hello host86-151-185-147.range86-151.btcentralplus.com [86.151.185.147]250-SIZE 52428800250-8BITMIME250-PIPELINING250-X_PIPE_CONNECT250-AUTH PLAIN LOGIN CRAM-MD5250-CHUNKING250-STARTTLS250 HELP
2021-03-04 10:29:43 CLIENT -> SERVER: STARTTLS
2021-03-04 10:29:43 SMTP INBOUND: "220 TLS go ahead"
2021-03-04 10:29:43 SERVER -> CLIENT: 220 TLS go ahead
2021-03-04 10:29:43 CLIENT -> SERVER: EHLO localhost
2021-03-04 10:29:44 SMTP INBOUND: "250-mta1.hi.local Hello host86-151-185-147.range86-151.btcentralplus.com [86.151.185.147]"
2021-03-04 10:29:44 SMTP INBOUND: "250-SIZE 52428800"
2021-03-04 10:29:44 SMTP INBOUND: "250-8BITMIME"
2021-03-04 10:29:44 SMTP INBOUND: "250-PIPELINING"
2021-03-04 10:29:44 SMTP INBOUND: "250-X_PIPE_CONNECT"
2021-03-04 10:29:44 SMTP INBOUND: "250-AUTH PLAIN LOGIN CRAM-MD5"
2021-03-04 10:29:44 SMTP INBOUND: "250-CHUNKING"
2021-03-04 10:29:44 SMTP INBOUND: "250 HELP"
2021-03-04 10:29:44 SERVER -> CLIENT: 250-mta1.hi.local Hello host86-151-185-147.range86-151.btcentralplus.com [86.151.185.147]250-SIZE 52428800250-8BITMIME250-PIPELINING250-X_PIPE_CONNECT250-AUTH PLAIN LOGIN CRAM-MD5250-CHUNKING250 HELP
2021-03-04 10:29:44 Auth method requested: PLAIN
2021-03-04 10:29:44 Auth methods available on the server: PLAIN,LOGIN,CRAM-MD5
2021-03-04 10:29:44 CLIENT -> SERVER: AUTH PLAIN
2021-03-04 10:29:44 SMTP INBOUND: "535 Incorrect authentication data"
2021-03-04 10:29:44 SERVER -> CLIENT: 535 Incorrect authentication data
2021-03-04 10:29:44 SMTP ERROR: AUTH command failed: 535 Incorrect authentication data
SMTP Error: Could not authenticate.
2021-03-04 10:29:44 CLIENT -> SERVER: QUIT
2021-03-04 10:29:44 SMTP INBOUND: "221 mta1.hi.local closing connection"
2021-03-04 10:29:44 SERVER -> CLIENT: 221 mta1.hi.local closing connection
2021-03-04 10:29:44 Connection: closed
SMTP Error: Could not authenticate.

这是我的电子邮件提供商提供的示例(即他们所期望的那种):

Client: EHLO client.example.com
Server: 250-smtp.example.com Hello client.example.com
Server: 250 AUTH GSSAPI DIGEST-MD5 PLAIN
Client: AUTH PLAIN dGVzdAB0ZXN0ADEyMzQ=
Server: 235 2.7.0 Authentication successful

区别似乎在于客户端到服务器的 AUTH PLAIN 消息:我的调试跟踪仅显示“AUTH PLAIN”,而 Pixel 的示例显示了一个编码字符串。我错过了什么?

【问题讨论】:

    标签: email smtp wamp phpmailer


    【解决方案1】:

    RFC4954 提供了两个AUTH PLAIN SMTP 身份验证机制的示例。第一个就像你看到的那样:

    S: 250 AUTH GSSAPI DIGEST-MD5 PLAIN
    C: AUTH PLAIN dGVzdAB0ZXN0ADEyMzQ=
    S: 235 2.7.0 Authentication successful
    

    第二种是相同机制的另一种形式:

    S: 250 AUTH GSSAPI DIGEST-MD5 PLAIN
    C: AUTH PLAIN
    S: 334
    C: dGVzdAB0ZXN0ADEyMzQ=
    S: 235 2.7.0 Authentication successful
    

    您可以看到,这两种方法提交了相同的身份验证字符串,但第二种方法在发送身份验证字符串之前,还有来自服务器的额外334 响应。虽然效率稍低,但它的优点是该方法与用于类似的LOGIN 身份验证机制的方法相同,后者也使用334 中间响应。

    PHPMailer 为PLAIN 实现了第二种机制。您的服务器显然不喜欢这样,我将其描述为错误;它应该允许两种模式。

    一个简单的解决方案是切换到使用您的服务器也支持的LOGIN 机制,并且只有一个版本:

    $mail->AuthType = 'LOGIN';
    

    您也可以使用您的服务器和 PHPMailer 都支持的CRAM-MD5,并且稍微安全一些,尽管通过 TLS 并没有太大区别。

    【讨论】:

    • 感谢 Synchro - 在发布我的回复之前我没有看到您的回复 - 我可能应该先刷新页面!我的结论(在查看 PHPMailer 代码之后 - 特别是 SMTP.php 中的身份验证函数)与您在此处描述的几乎相同。我切换到 CRAM-MD5,服务器挑战按预期工作。尽管服务器报告它支持所有三个,但我还没有尝试过 LOGIN。非常感谢您的回复。我会把你标记为答案!
    【解决方案2】:

    已排序。我发送到的服务器上的 AUTH PLAIN 协议有问题。我将身份验证类型更改为 CRAM-MD5,一切正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-23
      • 2016-09-22
      • 2016-07-30
      • 1970-01-01
      相关资源
      最近更新 更多