2009-08-05 53 views
16

我無法使用PHPMailer類發送電子郵件,但它可以與PEAR Mail :: factory一起使用。使用PHPMailer和PHP郵件進行SMTP身份驗證的SMTP驗證問題作品

我想問題是與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 = '[email protected]'; // a valid email here 
$mail->Password = '***'; // the password from email 
$mail->From = '[email protected]'; 
$mail->SMTPDebug = true; 
$mail->AddReplyTo('[email protected]', 'Test'); 

$mail->FromName = 'Test SMTP'; 
$mail->AddAddress('[email protected]', '[email protected]'); 

$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; 
} 

?> 

梨,工程的代碼是:

<?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' => '[email protected]', 
       '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' => '[email protected]', 
    'password' => '***')); 

$mail->send('[email protected]', $hdrs, $body); 
?> 

當我運行與調試激活(SMTPDebug =真)我有問題的代碼:

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. 

一nd 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: [email protected] 

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 

回答

0

表示OpenSSL擴展啓用並且目錄語言帶「br」? 首先檢查數據。

+0

在在phpinfo()函數,我有: ... OpenSSL支持\t已啓用 OpenSSL版本\t OpenSSL 0.9.8e-fips-rhel5 2008年7月1日 是什麼讓我瘋了,它是與PEAR的工作。 – Evandro 2009-08-05 14:29:00

16

嘗試添加此:

$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將它放在正確的trac ķ。
另外,請確保您使用PHPMailer的最新版本。

+1

這個答案解決了我的問題。我的託管公司告訴我不要放置SSL,但後來我想TLS是必需的,他們忘了告訴我:p。 – Sebas 2014-05-07 14:01:27

0

Exim 4要求僅在客戶端發出EHLO後才發送AUTH命令 - 嘗試進行身份驗證而不使用EHLO將被拒絕。一些郵件服務器要求EHLO發佈兩次。 PHPMailer顯然沒有這樣做。如果PHPMailer不允許你強制啓動EHLO,那麼你應該切換到SwiftMailer 4

+0

爲了記錄,PHPMailer自動執行正確的操作並在STARTTLS之後重新發送EHLO。 – Synchro 2014-07-04 15:49:54

38

,我的意見解決了這一行

//$mail->IsSmtp(); 

奇怪的問題白衣最後PHPMailer的版本(5.2)

+1

註釋該行會生成輸出,就好像電子郵件已發送一樣,但實際上不會發送此類電子郵件。 – 2013-11-03 13:32:54

+0

不適合我......我知道這很奇怪,但是這段代碼在我的製作環境中,並且所有的電子郵件都被髮送了......(但是我的phpmailer是1歲) – maurox 2013-11-04 14:07:35

+1

發生這種情況時,發生這樣的問題: 無法實例化郵件功能。 梅勒錯誤:無法實例化郵件功能。 – a77icu5 2013-12-18 19:49:39

0

檢查是否已設置限制傳出SMTP於某些系統用戶(根,MTA,郵遞員......)。該限制可能會阻止垃圾郵件發送者,但會將傳出的SMTP連接重定向到本地郵件服務器。

2

這也發生在我身上。對我而言,Postfix與PHP腳本位於同一服務器上,並且當我使用SMTP身份驗證和smtp.domain.com而不是localhost時發生錯誤。

所以,當我註釋掉這些行:

$ MAIL-> SMTPAUTH = TRUE;
$ mail-> SMTPSecure =「tls」;

,並設置主機

$mail->Host = "localhost"; 

代替

$ MAIL->主機= 'smtp.mydomainiuse.com'

和它的工作:)