0

I'm trying to build a php script which can check microsoft mail login working or not, i use PHPMailer class https://github.com/PHPMailer/PHPMailer

here is my code

date_default_timezone_set('Etc/UTC');

require '../PHPMailerAutoload.php';

//Create a new SMTP instance
$smtp = new SMTP;

//Enable connection-level debug output
$smtp->do_debug = SMTP::DEBUG_CONNECTION;

try {
//Connect to an SMTP server
    if ($smtp->connect('smtp.office365.com', 587)) {
        //Say hello
        if ($smtp->hello('localhost')) { //Put your host name in here
            //Authenticate
            if ($smtp->authenticate('test1@sjnewman.co.uk', 'Kara9802')) {
                echo "Connected ok!";
            } else {
                throw new Exception('Authentication failed: ' . $smtp->getLastReply());
            }
        } else {
            throw new Exception('HELO failed: '. $smtp->getLastReply());
        }
    } else {
        throw new Exception('Connect failed');
    }
} catch (Exception $e) {
    echo 'SMTP error: '. $e->getMessage(), "\n";
}
//Whatever happened, close the connection.
$smtp->quit(true);

The script return debug output and says Authentication failed:

2016-03-19 13:08:49 Connection: opening to smtp.office365.com:587, timeout=30, options=array ( ) 2016-03-19 13:08:49 Connection: opened 2016-03-19 13:08:49 
SERVER -> CLIENT: 220 BLUPR19CA0034.outlook.office365.com Microsoft ESMTP MAIL Service ready at Sat, 19 Mar 2016 13:08:48 +0000 2016-03-19 13:08:49 CLIENT -> SERVER: EHLO localhost 2016-03-19 13:08:49 
SERVER -> CLIENT: 250-BLUPR19CA0034.outlook.office365.com Hello [204.93.163.59] 250-SIZE 157286400 250-PIPELINING 250-DSN 250-ENHANCEDSTATUSCODES 250-STARTTLS 250-8BITMIME 250-BINARYMIME 250 CHUNKING SMTP error: Authentication failed: 250-BLUPR19CA0034.outlook.office365.com Hello [204.93.163.59] 250-SIZE 157286400 250-PIPELINING 250-DSN 250-ENHANCEDSTATUSCODES 250-STARTTLS 250-8BITMIME 250-BINARYMIME 250 CHUNKING 2016-03-19 13:08:49 CLIENT -> SERVER: QUIT 2016-03-19 13:08:49 
SERVER -> CLIENT: 221 2.0.0 Service closing transmission channel 2016-03-19 13:08:49 Connection: closed 

But when i use this same logins for smtp send mail on PHPMailer it sending mails, i'm not sure why its not working for above script

anyone know how to fix this.

Salinda Bandara
  • 203
  • 7
  • 20

1 Answers1

1

Very simple: this server, like most servers, does not support authentication without encryption. Notice that the list of capabilities in its response does not include AUTH, but does include STARTTLS. So, you need to do that (and repeat your EHLO) before trying to auth. Regular PHPMailer does this for you, so it works. If you look at the souce of the smtpConnect function in PHPMailer you'll see how it does it there, and you need to pick the basics out of that.

Synchro
  • 35,538
  • 15
  • 81
  • 104