[php] Problem with SMTP authentication in PHP using PHPMailer, with Pear Mail works

I´m having trouble sending a e-mail with the PHPMailer class, but it works with PEAR Mail::factory.

I guess the problem is with the SMTP authentication, but I couldn´t find the problem.

The code with problem is:

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

?>

The code with PEAR that works is:

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

When I run the code with problem with debug activated (SMTPDebug = True) I got:

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.

And the debug result of the PEAR Mail code is:

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

This question is related to php email smtp pear phpmailer

The answer is


This happened to me as well. For me, Postfix was located at the same server as the PHP script, and the error was happening when I would be using SMTP authentication and smtp.domain.com instead of localhost.

So when I commented out these lines:

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

and set the host to

$mail->Host = "localhost";

instead

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

and it worked :)


that the OpenSSL extension enabled and the directory languages with "br"? first checks the data.


Try adding this:

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

By looking at your debug logs, you can notice that the failing PhpMailer log shows this:

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

While your successful PEAR log shows this:

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

My guess is that explicitly asking PHPMailer to use TLS will put it on the right track.
Also, make sure you're using the latest versin of PHPMailer.


Check if you have set restrict outgoing SMTP to only some system users (root, MTA, mailman...). That restriction may prevent the spammers, but will redirect outgoing SMTP connections to the local mail server.


strange issue that i solved by comment this line

//$mail->IsSmtp();

whit the last phpmailer version (5.2)


Exim 4 requires that AUTH command only be sent after the client issued EHLO - attempts to authenticate without EHLO would be rejected. Some mailservers require that EHLO be issued twice. PHPMailer apparently fails to do so. If PHPMailer does not allow you to force EHLO initiation, you really should switch to SwiftMailer 4.


This isn't on the code parter it's on the server side Contact your Server Manager or fix it from server if you own it If you use CPANEL/WHM GO TO WHM/SMTP RESTRICTIONS AND DISABLE IT


Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to email

Monitoring the Full Disclosure mailinglist require(vendor/autoload.php): failed to open stream Failed to authenticate on SMTP server error using gmail Expected response code 220 but got code "", with message "" in Laravel How to to send mail using gmail in Laravel? Laravel Mail::send() sending to multiple to or bcc addresses Getting "The remote certificate is invalid according to the validation procedure" when SMTP server has a valid certificate How to validate an e-mail address in swift? PHP mail function doesn't complete sending of e-mail How to validate email id in angularJs using ng-pattern

Examples related to smtp

5.7.57 SMTP - Client was not authenticated to send anonymous mail during MAIL FROM error PHPMailer - SMTP ERROR: Password command failed when send mail from my server php function mail() isn't working Gmail Error :The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required "An attempt was made to access a socket in a way forbidden by its access permissions" while using SMTP Getting error while sending email through Gmail SMTP - "Please log in via your web browser and then try again. 534-5.7.14" SmtpException: Unable to read data from the transport connection: net_io_connectionclosed How to configure SMTP settings in web.config Send mail via CMD console Mail not sending with PHPMailer over SSL using SMTP

Examples related to pear

Installing PHP Zip Extension Problem with SMTP authentication in PHP using PHPMailer, with Pear Mail works Getting PEAR to work on XAMPP (Apache/MySQL stack on Windows)

Examples related to phpmailer

require(vendor/autoload.php): failed to open stream Fatal error: Class 'PHPMailer' not found PHPMailer - SMTP ERROR: Password command failed when send mail from my server Getting error while sending email through Gmail SMTP - "Please log in via your web browser and then try again. 534-5.7.14" Mail not sending with PHPMailer over SSL using SMTP sending email via php mail function goes to spam Unable to send email using Gmail SMTP server through PHPMailer, getting error: SMTP AUTH is required for message submission on port 587. How to fix? phpmailer - The following SMTP Error: Data not accepted Send File Attachment from Form Using phpMailer and PHP phpmailer: Reply using only "Reply To" address