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'))
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.