2009-08-05 10 views
16

Ho problemi a inviare un'e-mail con la classe PHPMailer, ma funziona con PEAR Mail :: factory.Problemi con l'autenticazione SMTP in PHP utilizzando PHPMailer, con Pear Mail funziona

Immagino che il problema sia con l'autenticazione SMTP, ma non ho trovato il problema.

Il codice con il problema è:

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

?> 

Il codice con PEAR che funziona è:

<?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); 
?> 

Quando eseguo il codice con il problema con il debug attivato (SMTPDebug = True) ho ricevuto:

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. 

A ND il risultato di debug del codice 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: [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 

risposta

0

che le lingue directory con "br" l'estensione OpenSSL abilitato e? prima controlla i dati.

+0

In il phpinfo() che ho: ... Supporto OpenSSL \t abilitato OpenSSL Versione \t OpenSSL 0.9.8e-fips-rhel5 01 lug 2008 Ciò che mi fa impazzire è che con PEAR funziona. – Evandro

16

Prova ad aggiungere questo:

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

Osservando i tuoi registri di debug, si può notare che il registro PHPMailer mancanza di mostra questo:

(..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..) 

Mentre il registro PEAR successo lo dimostra:

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

La mia ipotesi è che chiedere esplicitamente a PHPMailer di usare TLS lo metterà sulla destra trac K.
Inoltre, assicurati di utilizzare l'ultima versione di PHPMailer.

+1

questa risposta ha risolto il mio problema. La mia società di hosting mi ha detto di non inserire SSL, ma suppongo che TLS fosse necessario e si sono dimenticati di dirmi: p – Sebas

0

Exim 4 richiede che il comando AUTH venga inviato solo dopo che il client ha emesso EHLO - i tentativi di autenticazione senza EHLO verrebbero rifiutati. Alcuni server di posta richiedono che EHLO venga emesso due volte. PHPMailer apparentemente non riesce a farlo. Se PHPMailer non ti consente di forzare l'avvio di EHLO, dovresti davvero passare a SwiftMailer 4.

+0

Solo per la cronologia, PHPMailer fa la cosa giusta automaticamente e invia di nuovo EHLO dopo STARTTLS. – Synchro

38

strano problema che ho risolto da commentare questa linea

//$mail->IsSmtp(); 

briciolo la versione ultima phpmailer (5,2)

+1

commentando la riga genera l'output come se l'e-mail fosse stata inviata, ma in realtà nessuna di tali e-mail è stata inviata. –

+0

non per me ... Lo so che è strano, ma questo codice è nel mio ambiente di produzione e tutte le email vengono inviati ... (ma il mio phpmailer è 1 anno di età) – maurox

+1

Questo accade quando commento che la linea: Impossibile creare un'istanza funzione di posta Errore Mailer: impossibile istanziare la funzione di posta. – a77icu5

0

Verificare se è stata impostata limitare SMTP in uscita solo ad alcuni utenti del sistema (root, MTA, postino...). Tale restrizione potrebbe impedire gli spammer, ma reindirizzerà le connessioni SMTP in uscita al server di posta locale.

2

Questo è successo anche a me. Per me, Postfix si trovava nello stesso server dello script PHP e l'errore stava accadendo quando avrei usato l'autenticazione SMTP e smtp.domain.com invece di localhost.

Così, quando ho commentato queste righe:

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

e impostare l'host per

$mail->Host = "localhost"; 

invece

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

e ha funzionato :)

Problemi correlati