'Unable to connect with TLS encryption with Laravel 5.5 Swiftmailer

I am trying to send a mail using the Laravel inbuilt swift mailer but I keep getting the "Unable to connect with TLS encryption" error everytime. Below is my .env file:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.office365.com
MAIL_PORT=587
MAIL_USERNAME=myemailaddress
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=tls

My config/mail.php file is below:

'driver' => env('MAIL_DRIVER', 'smtp'),

    'host' => env('MAIL_HOST', 'smtp.office365.com'),
    'port' => env('MAIL_PORT', 587),
    'from' => [
        'address' => env('MAIL_FROM_ADDRESS', 'myemailaddress'),
        'name' => env('MAIL_FROM_NAME', ''),
    ],

    'encryption' => env('MAIL_ENCRYPTION', 'tls'),

    'username' => env('MAIL_USERNAME','myemailaddress'),

    'password' => env('MAIL_PASSWORD','mypassword'),


    'sendmail' => '/usr/sbin/sendmail -bs',

    'markdown' => [
        'theme' => 'default',

        'paths' => [
            resource_path('views/vendor/mail'),
        ],
    ],

];

My controller where I initiate the mail sending is below:

if ($loan->save()) {
        $name = staff::select('staff_name')->where('staff_id','=',$id)->first();
        $data = array(
          'Name' => $name,
          'Amount' => $request->input('amount'),
          'Tenor' => $request->input('tenor'),
          'Purpose' => $request->input('purpose')
        );

        Mail::send('emails.loanrequest', $data, function($message){
          $message->from('myaddress');
          $message->to('myemailaddress');
          $message->subject('New Loan Request');
        });
      }

Any help that would make this work would be appreciated. Thanks..



Solution 1:[1]

Apparently, It was my proxy server that was blocking my connection. Once I switched my connection, it worked..

Solution 2:[2]

I had a similar problem trying to send an email using Laravel. Finally, I discovered that the problem was my antivirus (Avast in my case) software, blocking TLS calls for me. I write this because maybe it could be helpful to someone.

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 Kingsley Abia
Solution 2 Karl Hill