Getting an auth error, I need help

Anonymous
2024-09-25T12:22:47+00:00

Hi There,

I am a dev, and i have setup symfony which was working many many years, and now all sudden i get this error when i debug, when my system try's to send an email to an smtp using my smtp details:

Failed to send mail (eStore Test Email - to: bradley.france@exmaple): Symfony\Component\Mailer\Exception\TransportException: Failed to authenticate on SMTP server with username "******@hotmail.com" using the following authenticators: "LOGIN", "XOAUTH2". Authenticator "LOGIN" returned "Expected response code "235" but got code "535", with message "535 5.7.139 Authentication unsuccessful, basic authentication is disabled. [CWLP265CA0382.GBRP265.PROD.OUTLOOK.COM 2024-09-25T12:09:41.962Z 08DCDC380035C48B]".". Authenticator "XOAUTH2" returned "Expected response code "235" but got code "535", with message "535 5.7.3 Authentication unsuccessful [CWLP265CA0382.GBRP265.PROD.OUTLOOK.COM 2024-09-25T12:09:52.165Z 08DCDC380035C48B]".".

I have an app password already setup and i have tried to test with that password instead but it keeps saying failed and this error above. I'm using port 587 with the server smtp.office365.com, bare in mind it was working just with the username and basic password and then all suddenly has now stopped.

Outlook | Windows | Classic Outlook for Windows | For home

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

2 answers

Sort by: Most helpful
  1. Anonymous
    2024-10-28T04:43:25+00:00

    Hi Eleni, are you able to please assist me? I have tried everything I can and reached out to the help support and no one was able to assist.

    I used to use Gmail to "send email as" with my outlook email address. Since the update to remove Basic Authentication, I haven't been able to set this up again. I have turned 2FA on, set up app passwords, and tried every single combination of the SMTP server (smtp-mail.outlook.com and smtp.office365.com) and both ports (587 and 465). I have tried to follow the above instructions but still having issues.

    From your above instructions I have signed up to Microsoft Azure and followed the steps to create certificates & secrets and API permissions (SMTP.Send). I tried to go back to Gmail and set it up again, and still no luck. I just keep getting the same error:

    Authentication failed. Please check your username/password. <br>Server returned an error: "334 VXNlcm5hbWU6 334 UGFzc3dvcmQ6 535 5.7.139 Authentication unsuccessful, basic authentication is disabled. [CH5P223CA0016.NAMP223.PROD.OUTLOOK.COM 2024-10-28T04:35:13.663Z 08DCF5B0BD8CAFBF] , code: 535"

    If you could please assist in any way, it would be greatly appreciated. It is just a personal email address, nothing fancy, I just want to be able to "send email as" via Gmail. Thank you.

    Was this answer helpful?

    3 people found this answer helpful.
    0 comments No comments
  2. Anonymous
    2024-09-25T13:41:36+00:00

    Hello bradley f1,

    Thank you for reaching out to the Microsoft Community regarding your issue with sending emails using the Symfony framework. From the error message you provided, it seems like your application is facing authentication issues with the SMTP server, specifically Office365. This issue is not uncommon and can arise when the email service provider (in this case, Microsoft) makes security changes that impact how authentication works.

    The error you're encountering, "Failed to authenticate on SMTP server using the following authenticators: LOGIN, XOAUTH2," along with the "535 5.7.139 Authentication unsuccessful" message, strongly indicates that Microsoft has disabled basic authentication for your account. As part of its ongoing efforts to improve security, Microsoft has been deprecating support for basic authentication (username/password) and moving towards modern authentication (OAuth2).

    You mentioned that it was working previously and suddenly stopped, which aligns with Microsoft's timeline of gradually phasing out basic authentication across Office365 and Exchange services.

    You've already made commendable efforts by:

    • Setting up an app password, which is required when using basic authentication with two-factor authentication enabled.
    • Using the correct port (587) and the correct server (smtp.office365.com).
    • Attempting to troubleshoot using a valid app password.

    Given that Microsoft has likely disabled basic authentication for your account, here are some potential solutions and workarounds:

    Please refer to these Microsoft articles:

    Authenticate an IMAP, POP or SMTP connection using OAuth | Microsoft Learn

    Deprecation of Basic authentication in Exchange Online | Microsoft Learn

    Enable Modern Authentication (OAuth2)

    • Basic authentication is deprecated, so you'll need to migrate to modern authentication (OAuth2) for your email sending functionality. Symfony's Mailer component supports OAuth2, but you'll need to implement it using Microsoft's OAuth2 flow.
    • Migrating to OAuth2 is crucial since Microsoft is phasing out basic authentication for security reasons. Below are the detailed steps to implement OAuth2 authentication in Symfony for sending emails via Office365.

    You need to register your application with Azure AD to enable OAuth2 for your SMTP connection

    • Go to the Azure Portal and sign in using your Microsoft account.
    • Navigate to Azure Active Directory > App registrations.
    • Click New registration.
      • Name: Give your app a name (e.g., Symfony SMTP Mailer).
      • Supported account types: Select the appropriate type based on whether your app will be used within your organization or more broadly.
      • Redirect URI: Choose Public client/native (mobile & desktop) for now. You can change it later if needed.
    • Click Register.
    • After the registration, you’ll be taken to the app’s Overview page. Here, note down the Application (client) ID and Directory (tenant) ID. You’ll need these later.

    Create Client Secret

    • Navigate to Certificates & secrets.
    • Under Client secrets, click New client secret.
    • Give it a description (e.g., SMTP App Secret), and choose the expiration time.
    • Click Add and note down the Client Secret Value. You won’t be able to view it again, so save it securely.

    Grant SMTP Permissions

    API Permissions

    • Go to API permissions > Add a permission.
    • Select Microsoft Graph > Delegated permissions.
      • Search for SMTP.Send and select it.
    • After selecting, click Add permissions.

    Admin Consent

    • If required, click the Grant admin consent button to ensure the permissions are authorized by an admin in your organization.

    Configure Symfony to Use OAuth2

    To enable OAuth2 in Symfony’s Mailer component, you will use the client ID, secret, and tenant ID obtained earlier. Now we need to configure the Mailer service to send authenticated requests using OAuth2 tokens.

    Install the Required Packages

    First, ensure that you have the required Symfony Mailer packages installed.

    bash
    Copy code
    composer require symfony/mailer symfony/google-mailer symfony/http-client
    

    Install the OAuth2 Client Package

    You’ll also need the league/oauth2-client package for OAuth2 support.

    bash
    Copy code
    composer require league/oauth2-client
    

    Create a Token Provider

    Symfony’s Mailer component needs to fetch an OAuth2 token. You can create a service that does this for you.

    Create a Token Provider service in your Symfony project:

    php
    Copy code
    // src/Service/OutlookTokenProvider.php
    namespace App\Service;
    
    use Symfony\Component\HttpClient\HttpClient;
    use Symfony\Contracts\HttpClient\HttpClientInterface;
    use League\OAuth2\Client\Provider\GenericProvider;
    use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
    
    class OutlookTokenProvider
    {
        private $client;
        private $tokenUrl;
        private $clientId;
        private $clientSecret;
        private $tenantId;
    
        public function __construct(string $clientId, string $clientSecret, string $tenantId)
        {
            $this->tokenUrl = "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token";
            $this->clientId = $clientId;
            $this->clientSecret = $clientSecret;
            $this->tenantId = $tenantId;
            $this->client = HttpClient::create();
        }
    
        public function getAccessToken(): string
        {
            $response = $this->client->request('POST', $this->tokenUrl, [
                'body' => [
                    'client_id' => $this->clientId,
                    'client_secret' => $this->clientSecret,
                    'grant_type' => 'client_credentials',
                    'scope' => 'https://outlook.office365.com/.default',
                ]
            ]);
    
            $data = $response->toArray();
    
            return $data['access_token'];
        }
    }
    

    Update your services.yaml file to include the token provider service.

    yaml
    Copy code
    # config/services.yaml
    services:
        App\Service\OutlookTokenProvider:
            arguments:
                $clientId: '%env(OAUTH2_CLIENT_ID)%'
                $clientSecret: '%env(OAUTH2_CLIENT_SECRET)%'
                $tenantId: '%env(OAUTH2_TENANT_ID)%'
    

    Configure the Mailer Transport

    Now configure your Mailer to use this token provider. Symfony will use the token to authenticate against Office365's SMTP.

    Update your Mailer configuration in .env:

    dotenv
    Copy code
    # .env
    OAUTH2_CLIENT_ID=<your_client_id>
    OAUTH2_CLIENT_SECRET=<your_client_secret>
    OAUTH2_TENANT_ID=<your_tenant_id>
    MAILER_DSN=smtp://smtp.office365.com:587
    

    Modify the Mailer Configuration to Use OAuth2

    Update your Symfony Mailer transport to fetch and use OAuth2 tokens.

    php
    Copy code
    // src/Service/MailerService.php
    namespace App\Service;
    
    use Symfony\Component\Mailer\MailerInterface;
    use Symfony\Component\Mime\Email;
    use App\Service\OutlookTokenProvider;
    
    class MailerService
    {
        private $mailer;
        private $tokenProvider;
    
        public function __construct(MailerInterface $mailer, OutlookTokenProvider $tokenProvider)
        {
            $this->mailer = $mailer;
            $this->tokenProvider = $tokenProvider;
        }
    
        public function sendEmail(string $to, string $subject, string $content)
        {
            $token = $this->tokenProvider->getAccessToken();
    
            $transport = new EsmtpTransport('smtp.office365.com', 587);
            $transport->setUsername('<your_username>@domain.com');
            $transport->setPassword($token);
            $this->mailer = new Mailer($transport);
    
            $email = (new Email())
                ->from('<your_username>@domain.com')
                ->to($to)
                ->subject($subject)
                ->text($content);
    
            $this->mailer->send($email);
        }
    }
    

    Testing and Verifying

    Clear Cache and Test

    • Clear the cache using:bash
      Copy code
      php bin/console cache:clear
    • Now, test sending an email with the updated OAuth2-based configuration.
    • If your emails are still failing to send, review the logs in Symfony or your email provider to ensure OAuth2 tokens are being used correctly.

    Use an App Password (With Basic Auth Enabled)

    • If OAuth2 is not an immediate option, ensure that basic authentication is explicitly enabled for your Office365 account. Check with your Office365 administrator or through the Microsoft 365 Admin Center.
    • If you have multi-factor authentication (MFA) enabled on your account, continue using an app-specific password for email authentication. However, this method will likely stop working eventually as Microsoft phases out basic authentication entirely.

    Check SMTP Configuration

    • Ensure that your SMTP server configurations in Symfony are correct:yaml
      Copy code
      MAILER_DSN=smtp://<USERNAME>:<APP_PASSWORD>@smtp.office365.com:587?encryption=tls
      • Replace <USERNAME> and <APP_PASSWORD> with the respective values. The app password must be the one generated in your account settings.
    • Double-check any firewalls or network configurations that might block access to Office365's SMTP server.

    Disable Security Defaults (if applicable):

    • If your organization has security defaults enabled, it may be blocking basic authentication. While not recommended for security reasons, temporarily disabling these defaults may help you regain access.
    • To disable security defaults, log into the Azure portal and navigate to Azure Active Directory > Properties > Manage Security Defaults.

    Review Conditional Access Policies:

    • Check if there are any conditional access policies that might be affecting your ability to authenticate via SMTP. These policies can restrict access based on various conditions.

    Temporary Alternative: Use an External SMTP Provider

    • As a workaround, you might consider using another SMTP provider, such as SendGrid or Mailgun, while you transition to OAuth2. These providers support modern authentication and are easily integrable with Symfony.

    To resolve this issue permanently, migrating to OAuth2 authentication is recommended. If you need further assistance setting up OAuth2 with Symfony or have more questions, please don't hesitate to ask!

    Please understand that our initial response may not always resolve the issue immediately. However, with your help and more detailed information, we can work together to find a solution.

    Appreciate your patience and understanding and thank you for your time and cooperation. Have a great day!

    Sincerely,

    Microsoft Community Moderator

    Was this answer helpful?

    3 people found this answer helpful.
    0 comments No comments