Laravel 5.5通过gmail发送电子邮件,从数据库配置

I have a function to send emails and it usually works. Now I want to set configuration for gmail and I see error "expected response code 250 but got code "530", with message "530-5.5.1 Authentication Required. Learn more at 530 5.5.1 https://support.google.com/mail/?p=WantAuthError"

I have unlocked captcha and allow for less safe applications. When I use .env configuration all works. But in my case, I try to set configuration from the database.

protected function makeEmail($config, $data){
  try{
    $account =  Settings::getEmailItem($config->account);
    Config::set('mail.driver','smtp');
    Config::set('mail.host',$account['host']);
    Config::set('mail.port',$account['port']);
    Config::set('mail.password',$account['password']);
    Config::set('mail.username',$account['username']);
    Config::set('mail.from.name',$account['from']);
    Config::set('mail.from.address',$account['username']);
    //Config::set('mail.encryption','tls');

    $app = App::getInstance();
    $app->singleton(TransportManager::class, function($app){
      return new TransportManager($app);
    });

    $mailer = new Swift_Mailer($app['swift.transport']->driver());
    Mail::setSwiftMailer($mailer);

    $recipients =  collect(explode(',',$config->recipients))->map(function($recipient) use($data){
      $matches;
      if(preg_match('/^{{\$(.+)}}/',trim($recipient),$matches)){
        if(array_key_exists($matches[1],$data)){
          return $data[$matches[1]];
        }
      }else{
        return $recipient;
      }
    })->all();
    //dd(config('mail'));
    Mail::to($recipients)->send(new SendFreeEmail($config, $data));
  }catch(\Exception $e){
    dd($e->getMessage(), $e->getCode(), $e->getLine(), $e->getFile());
  }

When I display config('mail') there are my data from the database.

Bellow is an image from dd(config('mail'))

enter image description here

Thanks in advance for any hints.

EDIT: I solved this problem but it's weird. When I set as driver "sendemail" as driver it works.

Did you configure your gmail account settings ?

You need to keep Allow less secure apps : ON .

It seems your configurations are cached. php artisan config:clear command should solve your issue.

Laravel Documentation says:

If you execute the config:cache command during your deployment process, you should be sure that you are only calling the env function from within your configuration files. Once the configuration has been cached, the .env file will not be loaded and all calls to the env function will return null.