doutuan6158 2018-06-06 21:06
浏览 210

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.

  • 写回答

2条回答 默认 最新

  • dqyhj2014 2018-06-06 21:18
    关注

    Did you configure your gmail account settings ?

    You need to keep Allow less secure apps : ON .

    评论

报告相同问题?

悬赏问题

  • ¥15 对于这个问题的代码运行
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败