'php laravel swift email sending problems

I am trying to send an email threw a laravel controller

this is my code:

  $email_sender   = '[email protected]';
  $email_pass     = 'blablablablablabla';
  $email_to       = '[email protected]';

  // Backup your default mailer
  $backup = \Mail::getSwiftMailer();


      try{

         //https://accounts.google.com/DisplayUnlockCaptcha
         // Setup your gmail mailer

         $transport = (new Swift_SmtpTransport('smtp.gmail.com', 587))
         ->setUsername($email_sender)
         ->setPassword($email_pass);

         $mailer = new Swift_Mailer($transport);

         // Set t

he mailer as gmail
     \Mail::setSwiftMailer($gmail);

     $data['emailto'] = $email_sender;
     $data['sender'] = $email_to;
     //Sender dan Reply harus sama

     $message = (new Swift_Message('Reset Password'))
     ->setFrom([$email_sender => 'EndritSheholli'])
     ->setTo([$email_to , '[email protected]' => 'ESheholli'])
     ->setBody('Here is the message itself')
         ;
         $result = $mailer->send($message);

      }catch(\Swift_TransportException $e){
         $response = $e->getMessage() ;
         echo $response;
      }

the error that is returned is:

Class 'App\Http\Controllers\Swift_SmtpTransport' not found

I have installed the composer package and I have tried a lot of proposals in the internet but I cannot find a solution.



Solution 1:[1]

You must declare Swift_SmtpTransport in use section or write full namespace of class in your code. For example:

use Swift_SmtpTransport;


// other code 

or

$transport = new \Swift_SmtpTransport

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 Maksym Fedorov