'Laravel Office365 Expected response code 250 but got code "", with message ""

Need help setting up Mail on my Laravel application.

My .env file:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.office365.com
MAIL_PORT=587
MAIL_USERNAME=*****@*****.com
MAIL_PASSWORD=******
MAIL_ENCRYPTION=tls

This returns the following error :

Expected response code 250 but got code "", with message ""


Solution 1:[1]

I had the same problem and it turns out Office 365 doesn't let you use a different from setting in your mail sending function than the one you use in your .env file.

I wanted to set the from name and email address according to whatever data was coming in via the request of a form submission as to capture them in the CRM system and it threw that same exception. Not sure if this helps you, but this was how I solved my problem.

Here's my code in the controller as an example:


$data = [
   'name' => $request->get('name'),
   'surname' => $request->get('surname'),
   'email' => $request->get('email'),
   'phone' => $request->get('phone'),
    'subject' => $request->get('message-subject')
];

$from = $data['name'] . ' ' . $data['surname'];
            Mail::send('mail.mymail', compact('data'), function($message) use ($request, $data, $from) {
                                $message->to('[email protected]', 'Some Company I Work For')->subject('Website Contact Form');
                                $message->from($data['email'], $from);
// Office 365 won't let you use a different from address here,so use same email as per your .env
            });

Solution 2:[2]

We were getting the same error message in one of our customer's internal tool, which runs on Symfony 3 and Swiftmailer v5.4.4.

After some debugging, I found out that there was a previous exception thrown by Swiftmailer. The reason was the response from smtp.office365.com:

421 4.7.66 TLS 1.0 and 1.1 are not supported. Please upgrade/update your client to support TLS 1.2. Visit https://aka.ms/smtp_auth_tls. [AM6PR02CA0020.eurprd02.prod.outlook.com]

Microsoft dropped support for TLS 1.0 und 1.1. After some research I found this github issue:

This commit (4c4b333) introduced TLS 1.1 / 1.2 support, but didn't make it to any release yet. Unfortunately all Swiftmailer users (besides the ones using recent dev-master) are tied to TLS 1.0, which starts to give issues

In one of the comments @fabpot replied that they've backported the bugfix to Swiftmailer 5. It landed in release 5.4.10.

After upgrading Swiftmailer, the mailing worked again.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 chilldsgn
Solution 2 naitsirch