SMTP Authentication Issues - App Password already enabled

Zafran 0 Reputation points
2023-02-26T09:13:56.64+00:00
Hello There,

Wondering if anyone has any experience with office 365 business, and using external apps to send mail through SMTP. I'm building a php script to send emails from my account, using phpmailer class, and I believe I've set everything up correctly. I've tried the same process with my googlemail account, and it works fine and sends the email correctly - unfortunately my office 365 email account does not. Shown below is my script, which worked fine when using google smtp, but not my business office 365 smtp. 

The failure is simply an authentication error in SMTP, and I'm sure the credentials are correct, as I log into the web environment with them. I've also tried producing an app password and using that, as I found something online about that - but no luck! Any help is greatly appreciated :) 

- Michael

<?php

require("/var/www/html/vendor/phpmailer/phpmailer/src/PHPMailer.php");

require("/var/www/html/vendor/phpmailer/phpmailer/src/SMTP.php");

require("/var/www/html/vendor/phpmailer/phpmailer/src/Exception.php");

$mail = new PHPMailer\PHPMailer\PHPMailer();

$mail->isSMTP();

$mail->Host = 'smtp.office365.com';

$mail->Port       = '587';

$mail->SMTPSecure = 'tls';

$mail->SMTPAuth   = true;

$mail->Username = '*** Email address is removed for privacy ***';

$mail->Password = '***********';

$mail->SetFrom('*** Email address is removed for privacy ***', 'FromEmail');

$mail->addAddress('*** Email address is removed for privacy ***', 'ToEmail');

$mail->SMTPDebug  = 3;

$mail->Debugoutput = function($str, $level) {echo "debug level $level; message: $str";}; //$mail->Debugoutput = 'echo';

$mail->IsHTML(true);


$mail->Subject = 'Test';

$mail->Body    = 'Test';

$mail->AltBody = 'Test';


if(!$mail->send()) {

    echo 'Message could not be sent.';

    echo 'Mailer Error: ' . $mail->ErrorInfo;

} else {

    echo 'Message has been sent';

}
Exchange Online
Exchange Online
A Microsoft email and calendaring hosted service.
6,171 questions
Microsoft Security Microsoft Authenticator
{count} votes

2 answers

Sort by: Most helpful
  1. Patchfox 4,176 Reputation points
    2023-02-26T09:47:11.9533333+00:00

    Hi Zafran, I hope I can help you with this question.

    Did you follow the steps in the official microsoft documentation to generate a app password

    https://support.microsoft.com/en-us/account-billing/manage-app-passwords-for-two-step-verification-d6dc8c6d-4bf7-4851-ad95-6d07799387e9

    Did you checked if SMTP Auth is activated for the sepcific mailbox

    Get-CASMailbox -Identity <MailboxIdentity> | Format-List SmtpClientAuthenticationDisabled

    If it is False, it's disabled, if it is True, it's not enabled, if it is blank, the global settings of the tenant are used

    If the global settings are used, please verify with these command, it the SMTP Auth is enabled:

    Get-TransportConfig | Format-List SmtpClientAuthenticationDisabled


    If the reply was helpful, please don’t forget to upvote or accept it as an answer, thank you.

    0 comments No comments

  2. Aholic Liang-MSFT 13,886 Reputation points Microsoft External Staff
    2023-02-27T08:18:35.3933333+00:00

    Hi @Zafran ,

    Please check on Exchange online that the following configuration is applied to allow send mail using SMTP AUTH client submission:

    1.Disable the Azure security default value: Providing a default level of security in Azure Active Directory - Microsoft Entra | Microsoft Learn

    2.Enable SMTP client authentication on the licensed mailbox you are using

    Set-CASMailbox -Identity ******@contoso.com -SmtpClientAuthenticationDisabled $false
    

    3.Disable Multi Factor Authentication (MFA) on the licensed mailbox being used:

    • In the Microsoft 365 admin center, in the left navigation menu choose Users > Active users.
    • On the Active users page, choose Multi-factor authentication.
    • On the Multi-factor authentication page, select the user and set the Multi-factor auth status to Disabled.

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.