setGmailMailer(); break; default: break; } } public function setGmailMailer() { $sending_user = $this->entity->client->getSetting('gmail_sending_user_id'); $user = User::find($sending_user); $google = (new Google())->init(); $google->getClient()->setAccessToken(json_encode($user->oauth_user_token)); if ($google->getClient()->isAccessTokenExpired()) { $google->refreshToken($user); } /* * Now that our token is refresh and valid we can boot the * mail driver at runtime and also set the token which will persist * just for this request. */ Config::set('mail.driver', 'gmail'); Config::set('services.gmail.token', $user->oauth_user_token->access_token); (new MailServiceProvider(app()))->register(); } }