php laravel快速电子邮件发送问题

时间:2018-12-13 13:46:17

标签: php laravel-5 swiftmailer

我正在尝试发送一封电子邮件,扔了一个laravel控制器

这是我的代码:

  $email_sender   = 'endritsheholli@gmail.com';
  $email_pass     = 'blablablablablabla';
  $email_to       = 'endritsheholli.a@gmail.com';

  // 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 , 'endritsheholli.a@gmail.com' => 'ESheholli'])
     ->setBody('Here is the message itself')
         ;
         $result = $mailer->send($message);

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

返回的错误是:

找不到类'App \ Http \ Controllers \ Swift_SmtpTransport'

我已经安装了composer软件包,并且在Internet上尝试了很多建议,但是找不到解决方案。

1 个答案:

答案 0 :(得分:2)

必须在使用部分声明Swift_SmtpTransport或在代码中编写类的完整名称空间。例如:

use Swift_SmtpTransport;


// other code 

$transport = new \Swift_SmtpTransport