【问题标题】:Problem with SMTP authentication in PHP using PHPMailer, with Pear Mail works使用 PHPMailer 的 PHP 中的 SMTP 身份验证问题,Pear Mail 有效
【发布时间】:2010-11-17 00:36:32
【问题描述】:

我在使用 PHPMailer 类发送电子邮件时遇到问题,但它适用于 PEAR Mail::factory。

我猜问题出在 SMTP 身份验证上,但我找不到问题所在。

有问题的代码是:

<?php
require("class.phpmailer.php");

$mail = new PHPMailer();

$mail->IsSMTP();       // set mailer to use SMTP
$mail->Host = 'mail.xxx.com.br';  // my host here
$mail->SMTPAuth = true;     // turn on SMTP authentication
$mail->Username = 'xxx@xxx.com.br';  // a valid email here
$mail->Password = '***';  // the password from email
$mail->From = 'from@xxx.com.br';
$mail->SMTPDebug = true;
$mail->AddReplyTo('from@xxx.com.br', 'Test');

$mail->FromName = 'Test SMTP';
$mail->AddAddress('teste@xxx.com.br', 'teste@xxx.com.br');

$mail->Subject = 'Test SMTP';
$mail->IsHTML(true);
$mail->Body = '<b>Teste</b><br><h1>teste 2</h1>';   
//$mail->Send();

if(!$mail->Send())
{
   echo "Message could not be sent. <p>";
   echo "Mailer Error: " . $mail->ErrorInfo;
   exit;
}

?>

使用 PEAR 的代码是:

<?php
include('Mail.php');
include('Mail/mime.php');

$text = 'Versao em texto';
$html = '<html><body>Versao de email em <b>HTML</b></body></html>';
$crlf = "\n";
$hdrs = array(
              'From'    => 'from@xxx.com.br',
              'Subject' => 'Test  - mail.php'
              );

$mime = new Mail_mime($crlf);

$mime->setTXTBody($text);
$mime->setHTMLBody($html);

$body = $mime->get();
$hdrs = $mime->headers($hdrs);

$mail = Mail::factory('smtp',
  array ('host' => 'mail.xxx.com.br',
    'debug'=> true,
    'auth' => true,
    'username' => 'xxx@xxx.com.br',
    'password' => '***'));

$mail->send('teste@xxx.com.br', $hdrs, $body);
?>

当我运行调试激活问题的代码时 (SMTPDebug = True),我得到:

SMTP -> FROM SERVER:

SMTP -> get_lines(): $data was ""

SMTP -> get_lines(): $str is "220-orion.bommtempo.net.br ESMTP Exim 4.69 #1 Wed, 05 Aug 2009 10:00:48 -0300 

"

SMTP -> get_lines(): $data is "220-orion.bommtempo.net.br ESMTP Exim 4.69 #1 Wed, 05 Aug 2009 10:00:48 -0300 

"

SMTP -> get_lines(): $data was "220-orion.bommtempo.net.br ESMTP Exim 4.69 #1 Wed, 05 Aug 2009 10:00:48 -0300 

"
SMTP -> get_lines(): $str is "220-We do not authorize the use of this system to transport unsolicited, 
"

SMTP -> get_lines(): $data is "220-orion.bommtempo.net.br ESMTP Exim 4.69 #1 Wed, 05 Aug 2009 10:00:48 -0300 

220-We do not authorize the use of this system to transport unsolicited, 

"

SMTP -> get_lines(): $data was "220-orion.bommtempo.net.br ESMTP Exim 4.69 #1 Wed, 05 Aug 2009 10:00:48 -0300 

220-We do not authorize the use of this system to transport unsolicited, 

"

SMTP -> get_lines(): $str is "220 and/or bulk e-mail.

"

SMTP -> get_lines(): $data is "220-orion.bommtempo.net.br ESMTP Exim 4.69 #1 Wed, 05 Aug 2009 10:00:48 -0300 

220-We do not authorize the use of this system to transport unsolicited, 

220 and/or bulk e-mail.

"

SMTP -> FROM SERVER: 

220-orion.bommtempo.net.br ESMTP Exim 4.69 #1 Wed, 05 Aug 2009 10:00:48 -0300 

220-We do not authorize the use of this system to transport unsolicited, 

220 and/or bulk e-mail.

SMTP -> ERROR: EHLO not accepted from server: 220-orion.bommtempo.net.br ESMTP Exim 4.69 #1 Wed, 05 Aug 2009 10:00:48 -0300 

220-We do not authorize the use of this system to transport unsolicited, 

220 and/or bulk e-mail.


SMTP -> get_lines(): $data was ""

SMTP -> get_lines(): $str is "250-orion.bommtempo.net.br Hello admin-teste.bommtempo.com.br [200.155.129.6]

"

SMTP -> get_lines(): $data is "250-orion.bommtempo.net.br Hello admin-teste.bommtempo.com.br [200.155.129.6]

"

SMTP -> get_lines(): $data was "250-orion.bommtempo.net.br Hello admin-teste.bommtempo.com.br [200.155.129.6]

"

SMTP -> get_lines(): $str is "250-SIZE 20971520

"

SMTP -> get_lines(): $data is "250-orion.bommtempo.net.br Hello admin-teste.bommtempo.com.br [200.155.129.6]
250-SIZE 20971520

"

SMTP -> get_lines(): $data was "250-orion.bommtempo.net.br Hello admin-teste.bommtempo.com.br [200.155.129.6]
250-SIZE 20971520

"

SMTP -> get_lines(): $str is "250-PIPELINING

"

SMTP -> get_lines(): $data is "250-orion.bommtempo.net.br Hello admin-teste.bommtempo.com.br [200.155.129.6]

250-SIZE 20971520

250-PIPELINING

"

SMTP -> get_lines(): $data was "250-orion.bommtempo.net.br Hello admin-teste.bommtempo.com.br [200.155.129.6]

250-SIZE 20971520

250-PIPELINING

"

SMTP -> get_lines(): $str is "250-AUTH PLAIN LOGIN

"

SMTP -> get_lines(): $data is "250-orion.bommtempo.net.br Hello admin-teste.bommtempo.com.br [200.155.129.6]

250-SIZE 20971520

250-PIPELINING

250-AUTH PLAIN LOGIN

"

SMTP -> get_lines(): $data was "250-orion.bommtempo.net.br Hello admin-teste.bommtempo.com.br [200.155.129.6]

250-SIZE 20971520

250-PIPELINING

250-AUTH PLAIN LOGIN

"

SMTP -> get_lines(): $str is "250-STARTTLS

"

SMTP -> get_lines(): $data is "250-orion.bommtempo.net.br Hello admin-teste.bommtempo.com.br [200.155.129.6]

250-SIZE 20971520

250-PIPELINING

250-AUTH PLAIN LOGIN

250-STARTTLS

"

SMTP -> get_lines(): $data was "250-orion.bommtempo.net.br Hello admin-teste.bommtempo.com.br [200.155.129.6]

250-SIZE 20971520

250-PIPELINING

250-AUTH PLAIN LOGIN

250-STARTTLS

"

SMTP -> get_lines(): $str is "250 HELP

"

SMTP -> get_lines(): $data is "250-orion.bommtempo.net.br Hello admin-teste.bommtempo.com.br [200.155.129.6]

250-SIZE 20971520

250-PIPELINING

250-AUTH PLAIN LOGIN

250-STARTTLS

250 HELP

"

SMTP -> FROM SERVER: 

250-orion.bommtempo.net.br Hello admin-teste.bommtempo.com.br [200.155.129.6]

250-SIZE 20971520

250-PIPELINING

250-AUTH PLAIN LOGIN

250-STARTTLS

250 HELP

SMTP -> get_lines(): $data was ""

SMTP -> get_lines(): $str is "250 orion.bommtempo.net.br Hello admin-teste.bommtempo.com.br [200.155.129.6]

"

SMTP -> get_lines(): $data is "250 orion.bommtempo.net.br Hello admin-teste.bommtempo.com.br [200.155.129.6]

"

SMTP -> ERROR: AUTH not accepted from server: 250 orion.bommtempo.net.br Hello admin-teste.bommtempo.com.br [200.155.129.6]


SMTP -> get_lines(): $data was ""

SMTP -> get_lines(): $str is "503 AUTH command used when not advertised

"

SMTP -> get_lines(): $data is "503 AUTH command used when not advertised

"

SMTP -> FROM SERVER:

503 AUTH command used when not advertised

SMTP -> ERROR: RSET failed: 503 AUTH command used when not advertised


Message could not be sent. <p>Mailer Error: SMTP Error: Could not connect to SMTP host.

PEAR Mail 代码的调试结果是:

DEBUG: Recv: 220-orion.bommtempo.net.br ESMTP Exim 4.69 #1 Tue, 04 Aug 2009 19:37:10 -0300 

DEBUG: Recv: 220-We do not authorize the use of this system to transport unsolicited, 

DEBUG: Recv: 220 and/or bulk e-mail.

DEBUG: Send: EHLO localhost


DEBUG: Recv: 250-orion.bommtempo.net.br Hello localhost [200.155.129.6]

DEBUG: Recv: 250-SIZE 20971520

DEBUG: Recv: 250-PIPELINING

DEBUG: Recv: 250-AUTH PLAIN LOGIN

DEBUG: Recv: 250-STARTTLS

DEBUG: Recv: 250 HELP

DEBUG: Send: STARTTLS


DEBUG: Recv: 220 TLS go ahead

DEBUG: Send: EHLO localhost


DEBUG: Recv: 250-orion.bommtempo.net.br Hello localhost [200.155.129.6]

DEBUG: Recv: 250-SIZE 20971520

DEBUG: Recv: 250-PIPELINING

DEBUG: Recv: 250-AUTH PLAIN LOGIN

DEBUG: Recv: 250 HELP

DEBUG: Send: AUTH LOGIN


DEBUG: Recv: 334 VXNlcm5hbWU6

DEBUG: Send: c2lzdGVtYWFkbWluQGJvbW10ZW1wby5jb20uYnI=


DEBUG: Recv: 334 UGFzc3dvcmQ6

DEBUG: Send: RVkyYVM4YnpMNU5k


DEBUG: Recv: 235 Authentication succeeded

DEBUG: Send: MAIL FROM:


DEBUG: Recv: 250 OK

DEBUG: Send: RCPT TO:


DEBUG: Recv: 250 Accepted

DEBUG: Send: DATA


DEBUG: Recv: 354 Enter message, ending with "." on a line by itself

DEBUG: Send: MIME-Version: 1.0

From: sistemaadmin@bommtempo.com.br

Subject: Teste de mail - mail.php

Content-Type: multipart/alternative;
    boundary="=_b3c5407ccf494306d78fbb35800efe65"


--=_b3c5407ccf494306d78fbb35800efe65

Content-Transfer-Encoding: 7bit

Content-Type: text/plain; charset="ISO-8859-1"


Versao em texto

--=_b3c5407ccf494306d78fbb35800efe65

Content-Transfer-Encoding: quoted-printable

Content-Type: text/html; charset="ISO-8859-1"


Versao de email em HTML--=_b3c5407ccf494306d78fbb35800efe65--

.

DEBUG: Recv: 250 OK id=1MYSd4-0005Ky-Jw

DEBUG: Send: QUIT

【问题讨论】:

    标签: php email smtp pear phpmailer


    【解决方案1】:

    这不在代码伙伴上,它在服务器端联系您的服务器管理器或从服务器修复它(如果您拥有它) 如果您使用 CPANEL/WHM 转到 WHM/SMTP 限制并禁用它

    【讨论】:

      【解决方案2】:

      这也发生在我身上。对我来说,Postfix 与 PHP 脚本位于同一台服务器上,当我使用 SMTP 身份验证和 smtp.domain.com 而不是 localhost 时发生错误。

      所以当我注释掉这些行时:

      $mail->SMTPAuth = true;
      $mail->SMTPSecure = "tls";

      并将主机设置为

      $mail->Host = "localhost";
      

      改为

      $mail->Host = 'smtp.mydomainiuse.com'

      它成功了:)

      【讨论】:

        【解决方案3】:

        检查您是否已将传出 SMTP 限制为仅限某些系统用户(root、MTA、mailman...)。该限制可能会阻止垃圾邮件发送者,但会将传出的 SMTP 连接重定向到本地邮件服务器。

        【讨论】:

          【解决方案4】:

          我通过评论这一行解决了一个奇怪的问题

          //$mail->IsSmtp();
          

          最新的 phpmailer 版本 (5.2)

          【讨论】:

          • 注释该行会生成输出,就好像发送了电子邮件但实际上没有发送这样的电子邮件。
          • 不适合我...我知道这很奇怪,但是这段代码在我的生产环境中并且所有电子邮件都已发送...(但我的 phpmailer 已经 1 岁了)
          • 当注释该行时会发生这种情况:无法实例化邮件功能。邮件程序错误:无法实例化邮件功能。
          • 当注释掉 $mail-&gt;IsSmtp(); 行时,邮件将不会使用 SMTP 发送。
          • 它可能使用 localhost 而不是 SMTP 吗?
          【解决方案5】:

          Exim 4 要求仅在客户端发出 EHLO 后发送 AUTH 命令 - 尝试在没有 EHLO 的情况下进行身份验证将被拒绝。一些邮件服务器要求 EHLO 发出两次。 PHPMailer 显然没有这样做。如果 PHPMailer 不允许你强制 EHLO 启动,你真的应该切换到SwiftMailer 4

          【讨论】:

          • 只是为了记录,PHPMailer 会自动做正确的事情并在 STARTTLS 之后重新发送 EHLO。
          【解决方案6】:

          尝试添加这个:

          $mail->SMTPAuth   = true;
          $mail->SMTPSecure = "tls";
          

          通过查看调试日志,您可以注意到失败的 PhpMailer 日志显示如下:

          (..snip..)
          SMTP -> ERROR: AUTH not accepted from server: 250 orion.bommtempo.net.br Hello admin-teste.bommtempo.com.br [200.155.129.6]
          (..snip..)
          503 AUTH command used when not advertised
          (..snip..)
          

          虽然您成功的 PEAR 日志显示了这一点:

          DEBUG: Send: STARTTLS
          DEBUG: Recv: 220 TLS go ahead
          

          我的猜测是明确要求 PHPMailer 使用 TLS 会使它走上正轨。
          另外,请确保您使用的是最新版本的 PHPMailer。

          【讨论】:

          • 这个答案解决了我的问题。我的托管公司告诉我不要放 SSL,但后来我想 TLS 是必需的,他们忘了告诉我:p
          【解决方案7】:

          是否启用了 OpenSSL 扩展和带有“br”的目录语言? 首先检查数据。

          【讨论】:

          • 在 phpinfo() 中,我有: ... OpenSSL 支持已启用 OpenSSL 版本 OpenSSL 0.9.8e-fips-rhel5 2008 年 7 月 1 日 是什么让我抓狂的是,它与 PEAR 一起工作。跨度>
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-09-08
          • 2015-07-23
          • 2015-10-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多