laravellaravel-5smtpgmailswiftmailer

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

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