简体   繁体   中英

PHPMailer fails using Office 365 SMTP

I am using this code, which I've been using for a long time with a Linux server for mail but recently moved to Office 365 and have updated the credentials.

I also added $email->SMTPSecure = ''; for 365.

$email = new PHPMailer();
$email->SMTPDebug = 2;
$email->IsSMTP();
$email->SMTPAuth = true;
$email->IsHTML(true);
$email->Host = '';
$email->Port = 587;
$email->Username = '';
$email->Password = '';

But I'm getting an error

SMTP -> FROM SERVER:220 LO2P265CA0058.outlook.office365.com Microsoft ESMTP MAIL Service ready at Sun, 3 Oct 2021 12:18:59 +0000
SMTP -> FROM SERVER: 250-LO2P265CA0058.outlook.office365.com Hello [***] 250-SIZE 157286400 250-PIPELINING 250-DSN 250-ENHANCEDSTATUSCODES 250-STARTTLS 250-8BITMIME 250-BINARYMIME 250-CHUNKING 250 SMTPUTF8
SMTP -> ERROR: AUTH not accepted from server: 504 5.7.4 Unrecognized authentication type [LO2P265CA0058.GBRP265.PROD.OUTLOOK.COM]
SMTP -> FROM SERVER:221 2.0.0 Service closing transmission channel
SMTP Connect() failed.

I used this tool to test the settings, and it works fine https://www.gmass.co/smtp-test

This looks like you're using an old version of PHPMailer, so upgrade . Your debug output doesn't include the client side traffic, so we can't see wheat your client is saying, however, I can guess. For some reason you have encryption disabled, and as you can see, the server does not list authentication amongst its capabilities in this initial unencrypted state:

250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-STARTTLS
250-8BITMIME
250-BINARYMIME
250-CHUNKING
250 SMTPUTF8

Normally PHPMailer will spot that a server offers STARTTLS and will enable encryption automatically, and I can't see why your code would not work with that unless you're using a really, really old version.

So, upgrade , then add this line:

$mail->SMTPSecure = 'tls';

After STARTTLS the client will issue a new EHLO command and you'll see a new set of capabilities which will include AUTH, and so you will be able to authenticate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM