Can't Connect to SMTP Host - Office 365

I’m trying to send from my web server through Office 365 mail, but the connection is getting closed. I’m using PPHList 3.3.8. I see some similar questions posted on various boards, but nothing I’ve followed seems to change the error.
I checked with my hosting provider, and they say they don’t have SMTP Restrictions active.
I have a Let’s Encrypt certificate on my domain, and the checks I’ve done with DNS seem to say my records are OK.

Any suggestions?
Thanks

EDIT:
I downloaded PHPMailer 6.0.7 to see if it could be used with the current version of PHPList. I read other messages that say that there are many changes, and PHPList will need significant work to use PHPMailer 6.
I do see in the current version of PHPMailer’s SMTP.php file that there are different transaction ID patterns for exim, sendmail, postfix, Microsoft_ESMTP, Amazon_SES, SendGrid and CampaignMonitor. I don’t see these in the files for PHPMailer version 5, although I may just have not been looking in the right place.

Connection: opening to smtp.office365.com:587, timeout=5, options=array ()
Connection: opened
SMTP -> get_lines(): $data is ""
SMTP -> get_lines(): $str is "220-pcluster23.hostdomain.com ESMTP Exim 4.91 #1 Fri, 08 Feb 2019 07:41:30 -0700 "
SMTP -> get_lines(): $data is "220-pcluster23.hostdomain.com ESMTP Exim 4.91 #1 Fri, 08 Feb 2019 07:41:30 -0700 "
SMTP -> get_lines(): $str is "220-We do not authorize the use of this system to transport unsolicited, "
SMTP -> get_lines(): $data is "220-pcluster23.hostdomain.com ESMTP Exim 4.91 #1 Fri, 08 Feb 2019 07:41:30 -0700 220-We do not authorize the use of this system to transport unsolicited, "
SMTP -> get_lines(): $str is "220 and/or bulk e-mail."
SERVER -> CLIENT: 220-pcluster23.hostdomain.com ESMTP Exim 4.91 #1 Fri, 08 Feb 2019 07:41:30 -0700 220-We do not authorize the use of this system to transport unsolicited, 220 and/or bulk e-mail.
CLIENT -> SERVER: EHLO domain.org
SMTP -> get_lines(): $data is ""
SMTP -> get_lines(): $str is "250-pcluster23.hostdomain.com Hello domain.org [XXX.154.46.140]"
SMTP -> get_lines(): $data is "250-pcluster23.hostdomain.com Hello domain.org [XXX.154.46.140]"
SMTP -> get_lines(): $str is "250-SIZE 52428800"
SMTP -> get_lines(): $data is "250-pcluster23.hostdomain.com Hello domain.org [184.154.46.140]250-SIZE 52428800"
SMTP -> get_lines(): $str is "250-8BITMIME"
SMTP -> get_lines(): $data is "250-pcluster23.hostdomain.com Hello domain.org [184.154.46.140]250-SIZE 52428800250-8BITMIME"
SMTP -> get_lines(): $str is "250-PIPELINING"
SMTP -> get_lines(): $data is "250-pcluster23.hostdomain.com Hello domain.org [184.154.46.140]250-SIZE 52428800250-8BITMIME250-PIPELINING"
SMTP -> get_lines(): $str is "250-AUTH PLAIN LOGIN"
SMTP -> get_lines(): $data is "250-pcluster23.hostdomain.com Hello domain.org [184.154.46.140]250-SIZE 52428800250-8BITMIME250-PIPELINING250-AUTH PLAIN LOGIN"
SMTP -> get_lines(): $str is "250-STARTTLS"
SMTP -> get_lines(): $data is "250-pcluster23.hostdomain.com Hello domain.org [184.154.46.140]250-SIZE 52428800250-8BITMIME250-PIPELINING250-AUTH PLAIN LOGIN250-STARTTLS"
SMTP -> get_lines(): $str is "250 HELP"
SERVER -> CLIENT: 250-pcluster23.hostdomain.com Hello domain.org [XXX.154.46.140]250-SIZE 52428800250-8BITMIME250-PIPELINING250-AUTH PLAIN LOGIN250-STARTTLS250 HELP
CLIENT -> SERVER: STARTTLS
SMTP -> get_lines(): $data is ""
SMTP -> get_lines(): $str is "220 TLS go ahead"
SERVER -> CLIENT: 220 TLS go ahead
SMTP Error: Could not connect to SMTP host.
CLIENT -> SERVER: QUIT
SMTP -> get_lines(): $data is ""

SMTP -> get_lines(): $str is ""

Connection: closed

What value do you have in config.php for PHPMAILER_SECURE ?

Hi Duncan,
Thanks for the reply.
Here’s what is in the Message Sending Options section of my config.php with the user and password changed.

define("PHPMAILERHOST",'smtp.office365.com');
$phpmailer_smtpuser = 'name@domain.org';
$phpmailer_smtppassword = password';
define('PHPMAILERPORT',587);
define('PHPMAILER_SECURE','tls');
define('PHPMAILER_SMTP_DEBUG', 4);

It looks to be a restriction of your server as I can connect to office365 using those settings. It then fails on authentication

SERVER -> CLIENT: 220 AM6P194CA0013.outlook.office365.com Microsoft ESMTP MAIL Service ready at Sat, 9 Feb 2019 20:05:16 +0000
CLIENT -> SERVER: EHLO dcameron.me.uk
SERVER -> CLIENT: 250-AM6P194CA0013.outlook.office365.com Hello [77.72.0.162]250-SIZE 157286400250-PIPELINING250-DSN250-ENHANCEDSTATUSCODES250-STARTTLS250-8BITMIME250-BINARYMIME250-CHUNKING250 SMTPUTF8
CLIENT -> SERVER: STARTTLS
SERVER -> CLIENT: 220 2.0.0 SMTP server ready
CLIENT -> SERVER: EHLO dcameron.me.uk
SERVER -> CLIENT: 250-AM6P194CA0013.outlook.office365.com Hello [77.72.0.162]250-SIZE 157286400250-PIPELINING250-DSN250-ENHANCEDSTATUSCODES250-AUTH LOGIN XOAUTH2250-8BITMIME250-BINARYMIME250-CHUNKING250 SMTPUTF8
CLIENT -> SERVER: AUTH LOGIN
SERVER -> CLIENT: 334 VXNlcm5hbWU6
CLIENT -> SERVER: bmFtZUBkb21haW4ub3Jn
SERVER -> CLIENT: 334 UGFzc3dvcmQ6
CLIENT -> SERVER: cGFzc3dvcmQ=
SERVER -> CLIENT:
SMTP ERROR: Password command failed:
SMTP Error: Could not authenticate.
CLIENT -> SERVER: QUIT
SERVER -> CLIENT: 535 5.7.3 Authentication unsuccessful [AM6P194CA0013.EURP194.PROD.OUTLOOK.COM]
SMTP ERROR: QUIT command failed: 535 5.7.3 Authentication unsuccessful [AM6P194CA0013.EURP194.PROD.OUTLOOK.COM]

A simple quote is missing

$phpmailer_smtppassword = password';

should be
A simple quote is missing

$phpmailer_smtppassword = 'password';
A simple quote is missing

Thanks, but the correct quote is in the real config.php file. I accidentally removed it when I took out the real password for the post here.

I’ve got this on a shared hosting system, so I only have a limited SSH terminal. It doesn’t let me run netstat.

I did a scan through


and it’s showing port 587 is open to my domain.

@pauldow From what I can see the error message is issued in only one place in phpmailer. You can try adding some extra debug statements to see exactly where it is failing
in file admin/PHPMailer/class.smtp.php replace the startTLS() function

public function startTLS()
{
    if (!$this->sendCommand('STARTTLS', 'STARTTLS', 220)) {
        $this->edebug('send command failed', self::DEBUG_CONNECTION);
        return false;
    }

    //Allow the best TLS version(s) we can
    $crypto_method = STREAM_CRYPTO_METHOD_TLS_CLIENT;

    //PHP 5.6.7 dropped inclusion of TLS 1.1 and 1.2 in STREAM_CRYPTO_METHOD_TLS_CLIENT
    //so add them back in manually if we can
    if (defined('STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT')) {
        $crypto_method |= STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT;
        $crypto_method |= STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT;
    }

    // Begin encrypted connection
    if (!stream_socket_enable_crypto(
        $this->smtp_conn,
        true,
        $crypto_method
    )) {
        $this->edebug('streamsocket failed', self::DEBUG_CONNECTION);
        return false;
    }

    $this->edebug('start TLS successful', self::DEBUG_CONNECTION);
    return true;
}

Set the smtp debug level to be 3 then send a test email. With your settings I get “start TLS successful” then it fails on authentication

Thank you very much for your help Duncan,

I’m using PHP 7.1. Since your script refers to PHP 5.6.7, is that a (or the) problem?
I got
Connection: opening to smtp.office365.com:587, timeout=5, options=array ()
Connection: opened
SERVER -> CLIENT: 220-pcluster23.stablehost.com ESMTP Exim 4.91 #1 Sun, 10 Feb 2019 14:34:02 -0700 220-We do not authorize the use of this system to transport unsolicited, 220 and/or bulk e-mail.
CLIENT -> SERVER: EHLO eaa1310.org
SERVER -> CLIENT: 250-pcluster23.stablehost.com Hello eaa1310.org [184.154.46.140]250-SIZE 52428800250-8BITMIME250-PIPELINING250-AUTH PLAIN LOGIN250-STARTTLS250 HELP
CLIENT -> SERVER: STARTTLS
SERVER -> CLIENT: 220 TLS go ahead
streamsocket failed
SMTP Error: Could not connect to SMTP host.
CLIENT -> SERVER: QUIT

You can see that this function call is failing but I don’t have the knowledge to know why that might be.
Try looking in the php or web server error logs for any more information, or ask your hosting company to help.

Well, it’s working now. I did another online chat with the support people at my hosting service, and they DID have SMTP restrictions active. Good thing I got a different person this time. As you can see in my original post, I asked if SMTP restrictions were active.
He did now turn it off for me, so I can send the mail out.

Duncan, thank you for your help with this. I hope you didn’t spend too much time on it.