问题
i have problem to connect through smtp to office365. I read all tips here but nothing help.
Debug:
2018-05-16 08:12:52 Connection: opening to smtp.office365.com:587, timeout=300, options=array ( )
2018-05-16 08:12:52 Connection: opened
2018-05-16 08:12:52 SERVER -> CLIENT: 220 CWLP265CA0229.outlook.office365.com Microsoft ESMTP MAIL Service ready at Wed, 16 May 2018 08:12:52 +0000
2018-05-16 08:12:52 CLIENT -> SERVER: EHLO localhost
2018-05-16 08:12:52 SERVER -> CLIENT: 250-CWLP265CA0229.outlook.office365.com Hello [109.81.243.32] 250-SIZE 157286400 250-PIPELINING 250-DSN 250-ENHANCEDSTATUSCODES 250-STARTTLS 250-8BITMIME 250-BINARYMIME 250-CHUNKING 250 SMTPUTF8
2018-05-16 08:12:52 CLIENT -> SERVER: STARTTLS
2018-05-16 08:12:52 SERVER -> CLIENT: 220 2.0.0 SMTP server ready
2018-05-16 08:12:52 CLIENT -> SERVER: EHLO localhost
2018-05-16 08:12:52 SERVER -> CLIENT: 250-CWLP265CA0229.outlook.office365.com Hello [109.81.243.32] 250-SIZE 157286400 250-PIPELINING 250-DSN 250-ENHANCEDSTATUSCODES 250-AUTH LOGIN XOAUTH2 250-8BITMIME 250-BINARYMIME 250-CHUNKING 250 SMTPUTF8
2018-05-16 08:12:52 CLIENT -> SERVER: AUTH LOGIN
2018-05-16 08:12:52 SERVER -> CLIENT: 334 VXNlcm5hbWU6
2018-05-16 08:12:52 CLIENT -> SERVER: xxx==
2018-05-16 08:12:52 SERVER -> CLIENT: 334 UGFzc3dvcmQ6
2018-05-16 08:12:52 CLIENT -> SERVER: xxx=
2018-05-16 08:12:58 SERVER -> CLIENT: 535 5.7.3 Authentication unsuccessful [CWLP265CA0229.GBRP265.PROD.OUTLOOK.COM]
2018-05-16 08:12:58 SMTP ERROR: Password command failed: 535 5.7.3 Authentication unsuccessful [CWLP265CA0229.GBRP265.PROD.OUTLOOK.COM]
2018-05-16 08:12:58 SMTP Error: Could not authenticate.
2018-05-16 08:12:58 CLIENT -> SERVER: QUIT
2018-05-16 08:12:58 SERVER -> CLIENT: 221 2.0.0 Service closing transmission channel
2018-05-16 08:12:58 Connection: closed
2018-05-16 08:12:58 SMTP connect() failed.
PHP CODE:
$mail = new PHPMailer;
$mail->SMTPDebug = 3;
$mail->isSMTP();
$mail->Host = 'smtp.office365.com';
$mail->SMTPAuth = true;
$mail->Username = 'MAIL@DOMAIN.TLD';
$mail->Password = 'PASS';
$mail->SMTPSecure = 'tls';
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('MAIL@DOMAIN.TLD');
$mail->addAddress('TOMAIL'); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
回答1:
You have to enable it in o365 admin panel, then wait 3 minutes and done
also check $mail->SMTPAuth = true;
Use the Microsoft 365 admin center to enable or disable SMTP AUTH on specific mailboxes
Open the Microsoft 365 admin center and go to Users > Active users. Select the user, and in the flyout that appears, click Mail. In the Email apps section, click Manage email apps. Verify the Authenticated SMTP setting: unchecked = disabled, checked = enabled. When you're finished, click Save changes.
来源:https://stackoverflow.com/questions/50365644/cant-connect-to-office365-smtp-via-phpmailer