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

时间:2018-06-06 21:06:27

标签: php laravel swiftmailer

我有发送电子邮件的功能,它通常有效。现在我想设置gmail的配置,我看到错误"预期的响应代码250但是得到了代码" 530",带有消息" 530-5.5.1需要验证。在\ r \ n了解更多信息 530 5.5.1 https://support.google.com/mail/?p=WantAuthError"

我已解锁验证码并允许不太安全的应用程序。 当我使用.env配置时,所有工作。但就我而言,我尝试从数据库中设置配置。

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());
  }

当我显示config('mail')时,我的数据来自数据库。

Bellow是来自dd(config('mail'))

的图片

enter image description here

提前感谢任何提示。

编辑:我解决了这个问题,但这很奇怪。当我设置为驱动程序" sendemail"作为司机工作。

2 个答案:

答案 0 :(得分:0)

您是否配置了Gmail帐户设置?

您需要保留允许不太安全的应用:ON。

答案 1 :(得分:0)

似乎您的配置已缓存。 php artisan config:clear命令可以解决您的问题。

Laravel Documentation说:

  

如果在部署过程中执行config:cache命令,则应确保只从配置文件中调用env函数。缓存配置后,将不会加载.env文件,并且对env函数的所有调用都将返回null。

相关问题