2026-07-15

Expected response code 235 but got code 535 laravel send email using office365 smtp

Stefan Bogdanescu

Stefan Bogdanescu

Founder & Senior Architect

Expected response code 235 but got code 535 laravel send email using office365 smtp

Expected Response Code 235 but got code 535: Troubleshooting SMTP Authentication with Office 365 in Laravel

As a senior developer, I’ve seen countless scenarios where seemingly simple email configurations lead to frustrating authentication errors. When setting up external services like Office 365 SMTP within a framework like Laravel, these issues often stem not from the code itself, but from subtle differences in how modern security protocols handle credentials.

Today, we are diving deep into a very specific and common problem: receiving an error code 535 instead of the expected 235 when trying to send mail via Office 365 SMTP through Laravel. Understanding this distinction is key to solving your authentication headache.

Diagnosing the Error: What do Codes 235 and 535 Mean?

When an application attempts to authenticate with an SMTP server, it communicates using specific response codes defined by the SMTP protocol.

  • Response Code 235 (OK): This typically signifies a successful response during the initial authentication handshake. The server acknowledges the request and is ready for the next command.
  • Response Code 535 (Authentication Failed): This code explicitly tells the client that the authentication attempt failed. In the context of SMTP, this means the username and password provided were rejected by the server.

Your error message—Expected response code 235 but got code "535", with message "535 Incorrect authentication data\r\n"—is the system reporting that while it was expecting a successful login acknowledgment (235), the server actively rejected the credentials (535). This points directly to an issue with the credentials themselves, often related to modern security policies implemented by Microsoft 365.

Root Causes for Office 365 SMTP Failures

While it seems like a simple password entry error, authenticating against modern services like Office 365 often involves more complex layers of security that can trip up standard SMTP setups. Here are the most common reasons you encounter this 535 error:

1. Multi-Factor Authentication (MFA) and App Passwords

The single biggest culprit when using Microsoft 365 credentials is Multi-Factor Authentication (MFA). If your Office 365 account requires MFA, standard password authentication via SMTP often fails unless specific application permissions are granted. For many modern setups, you must generate an App Password specifically for the application to use instead of your main account password.

2. Modern Authentication Protocols

Office 365 uses modern security protocols (like OAuth/SAML) for web access. While SMTP still works, some configurations require specific settings on the Exchange server side that are not easily configured via simple .env variables alone. Ensure that the account credentials you are using have been explicitly granted permission to send mail through an external application.

3. Incorrect Host or Port Configuration

Although less likely when the error is specifically 535, always double-check that your host (smtp.office365.com) and port (587 for STARTTLS) are perfectly correct, as misconfiguration can sometimes lead to generic authentication failures.

Practical Steps to Resolve the Issue

To fix this problem, follow these steps systematically:

Step 1: Verify Credentials (The Basics) First, ensure that the MAIL_USERNAME and MAIL_PASSWORD in your .env file are exactly correct for the Office 365 account you are using. Try logging into the O365 web portal with those exact credentials to confirm they work outside of Laravel.

Step 2: Implement App Passwords (The Most Likely Fix) If standard login fails, this is where you must intervene. Log into your Microsoft account security settings and generate an App Password. Use this generated, unique password in place of your regular account password in your .env file. This bypasses some of the stricter MFA requirements for application-level communication.

Step 3: Review Laravel Configuration Ensure your Laravel setup is using the standard configuration correctly. The code you provided for sending mail is structurally correct for the Laravel Mail facade:

Mail::send(array(), array(), function ($m) use ($templatecontent) {
    $m->from('customerservice@****.com', 'TEST');
    $m->to('test@gmail.com', 'Test')
        ->subject($templatecontent['subject'])
        ->setBody($templatecontent['content'], 'text/html');
});

If the issue persists after verifying App Passwords, investigate any internal network firewalls or security policies on your Office 365 tenant that might be blocking outbound SMTP connections from your server. Remember, robust application development relies on understanding these underlying communication protocols, much like how well-structured code is essential in Laravel projects.

Conclusion

The error 535 Incorrect authentication data when using Office 365 SMTP is almost always an authentication hurdle related to modern security measures (like MFA). By focusing your troubleshooting efforts on generating and utilizing App Passwords, you can successfully establish secure email communication between your Laravel application and Microsoft Exchange. Debugging these external service integrations requires moving beyond the syntax of the framework and understanding the strict protocols enforced by the mail server itself. Happy coding!

Note: Blog content is currently available in English.

Tags:

Enhance your marketing setup with your own email marketing platform.

Join the growing number of SaaS platforms using Laravel Mail to offer email marketing solutions to their customers.