Asked by:
Cannot authenticate during send-mail process via PHP-Script(O365 SMTP) - MFA Problem

Question
-
Hi Guys,
i have a problem and dont know why this is not working for me..
I have a website on Azure(app-service/linux) and want to configure a send mail process via PHPmailer(php).
I tried to create my connection with this official Microsoft document --> Link <--- i tried it with Option 1.This is my Code:
<?php session_start(); use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\SMTP; use PHPMailer\PHPMailer\Exception; // Load Composer's autoloader require 'vendor/autoload.php'; return function ($site, $pages, $page) { if(isset($_POST['submit'])) { try { // Instantiation and passing `true` enables exceptions $mail = new PHPMailer(true); //Server settings //$mail->SMTPDebug = SMTP::DEBUG_SERVER; $mail->SMTPDebug = 3; // Enable verbose debug output $mail->isSMTP(); // Send using SMTP $mail->Host = 'smtp.office365.com'; // Set the SMTP server to send through $mail->SMTPAuth = true; // Enable SMTP authentication $mail->Username = 'User@tenant.de'; // SMTP username $mail->Password = 'PASSWORD'; // SMTP password $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; //$mail->SMTPSecure = 'tls'; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted $mail->Port = 587; // TCP port to connect to //Recipients $mail->setFrom('Test@tenant.com', 'Mailer'); $mail->addAddress('Test@outlook.com', 'Joe User'); // Content $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>'; $mail->send(); }catch (Exception $e) { echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}"; } //header('Location: /kontakt'); } };
The Error-Log on the Website:
2019-10-25 14:10:43 Connection: opening to smtp.office365.com:587, timeout=300, options=array() 2019-10-25 14:10:43 Connection: opened 2019-10-25 14:10:43 SERVER -> CLIENT: 220 AM3PR07CA0137.outlook.office365.com Microsoft ESMTP MAIL Service ready at Fri, 25 Oct 2019 14:10:42 +0000 2019-10-25 14:10:43 CLIENT -> SERVER: EHLO My-Homepage.de 2019-10-25 14:10:43 SERVER -> CLIENT: 250-AM3PR07CA0137.outlook.office365.com Hello [23.100.0.119]250-SIZE 157286400250-PIPELINING250-DSN250-ENHANCEDSTATUSCODES250-STARTTLS250-8BITMIME250-BINARYMIME250-CHUNKING250 SMTPUTF8 2019-10-25 14:10:43 CLIENT -> SERVER: STARTTLS 2019-10-25 14:10:43 SERVER -> CLIENT: 220 2.0.0 SMTP server ready 2019-10-25 14:10:43 CLIENT -> SERVER: EHLO My-Homepage.de 2019-10-25 14:10:43 SERVER -> CLIENT: 250-AM3PR07CA0137.outlook.office365.com Hello [23.100.0.119]250-SIZE 157286400250-PIPELINING250-DSN250-ENHANCEDSTATUSCODES250-AUTH LOGIN XOAUTH2250-8BITMIME250-BINARYMIME250-CHUNKING250 SMTPUTF8 2019-10-25 14:10:43 CLIENT -> SERVER: AUTH LOGIN 2019-10-25 14:10:43 SERVER -> CLIENT: 334 VXNlcm5hbWU6 2019-10-25 14:10:43 CLIENT -> SERVER: <credentials hidden> 2019-10-25 14:10:43 SERVER -> CLIENT: 334 UGFzc3dvcmQ6 2019-10-25 14:10:43 CLIENT -> SERVER: <credentials hidden> 2019-10-25 14:10:49 SERVER -> CLIENT: 535 5.7.3 Authentication unsuccessful [AM3PR07CA0137.eurprd07.prod.outlook.com] 2019-10-25 14:10:49 SMTP ERROR: Password command failed: 535 5.7.3 Authentication unsuccessful [AM3PR07CA0137.eurprd07.prod.outlook.com] SMTP Error: Could not authenticate. 2019-10-25 14:10:49 CLIENT -> SERVER: QUIT 2019-10-25 14:10:49 SERVER -> CLIENT: 221 2.0.0 Service closing transmission channel 2019-10-25 14:10:49 Connection: closed SMTP Error: Could not authenticate. Message could not be sent. Mailer Error: SMTP Error: Could not authenticate.
when i check the AzureAD Login-Logs i get this:
..
I thought it wouldn't matter if MFA is activated by the user or not?
Even when i exclude the User from the "MFA for Admins Policy", i get an error with the "End user protection".What am i doing wrong?
I hope you can help me..Thanks!
Leon
PS: if i selected the wrong forum then sorry.. you can move it to the right one.
- Moved by Grace MacJones-MSFT Monday, October 28, 2019 10:19 PM question related to Azure MFA
Friday, October 25, 2019 2:45 PM
All replies
-
Hey LonBoy98,
You will need to generate an app password and then use an app password for that account, instead of the regular user password. See here on how to do this: https://support.office.com/en-us/article/create-an-app-password-for-office-365-3e7c860f-bda4-4441-a618-b53953ee1183
This app password serves as the second authentication method for the dual authentication.
Others have adopted a third party mailing system with complete DKIM/DMARC/SPF records in place for this third party sender.This eliminates the hassle of using an Office 365 SMTP account to send email.
Please remember to mark one of the responses as answer if your question has been answered. If not please let us know if there are anymore questions. Thanks
- Edited by Frank Hu MSFT Wednesday, October 30, 2019 1:52 AM
- Proposed as answer by Frank Hu MSFT Wednesday, October 30, 2019 1:52 AM
Wednesday, October 30, 2019 1:51 AM -
I will try this solution as soon as possible and mark it as correct answer if its works.
so thanks!
Leon
Wednesday, November 13, 2019 10:35 AM