使用PHPMailer在PHP中进行SMTP身份验证时出现问题,Pear Mail正常工作

时间:2009-08-05 13:33:39

标签: php email smtp pear phpmailer

我在使用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邮件代码的调试结果是:

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

7 个答案:

答案 0 :(得分:42)

我通过评论此行解决的奇怪问题

//$mail->IsSmtp();

使用最后一个phpmailer版本(5.2)

答案 1 :(得分:17)

尝试添加此内容:

$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的最新版本。

答案 2 :(得分:3)

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

所以当我评论这些内容时:

$ mail-&gt; SMTPAuth = true;
    $ mail-&gt; SMTPSecure =“tls”;

并将主机设置为

$mail->Host = "localhost";

代替

$ mail-&gt; Host ='smtp.mydomainiuse.com'

它有效:)

答案 3 :(得分:0)

启用OpenSSL扩展并使用“br”启用目录语言? 首先检查数据。

答案 4 :(得分:0)

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

答案 5 :(得分:0)

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

答案 6 :(得分:-1)

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