How to use database for mail settings in Laravel

后端 未结 4 786
伪装坚强ぢ
伪装坚强ぢ 2021-02-06 15:51

I\'d like to keep users away from editing configuration files, so I\'ve made web interface in admin panel for setting up Mail server, username, password, port, encryption.. I w

4条回答
  •  既然无缘
    2021-02-06 16:47

    @DigitLimit , method share() has been dropped since Laravel 5.4. I had to work-around this problem using other methods, and I am not sure they are perfect. Here is my registerSwiftTransport() method in CustomMailServiceProvider class.

    Firstly, we need to determine if code is not executed while calling app through command line: "if(strpos(php_sapi_name(), 'cli') === false)". If we don't check that and don't prevent setting new params in this case, Artisan will throw us errors in command line. Secondly, we need to get settings from database somehow. I did it using my method getSettingValue(), where first argument is setting key, and second argument is default value if setting is not found. As you see, I assigned settings to $this->app['config']['mail']. After that, I used singleton() method:

    protected function registerSwiftTransport(){
        if (strpos(php_sapi_name(), 'cli') === false) {
           $this->app['config']['mail'] = [
            'driver'        => Setting::getSettingValue('mail_driver', '****'),
            'host'          => Setting::getSettingValue('mail_host', '****'),
            'port'          => Setting::getSettingValue('mail_port', 25),
            'from'          => [
                'address'   => Setting::getSettingValue('mail_from_address', '****'),
                'name'      => Setting::getSettingValue('mail_from_name', '****'),
            ],
            'encryption'    => Setting::getSettingValue('mail_encryption', '***'),
            'username'      => Setting::getSettingValue('mail_username', '****'),
            'password'      => Setting::getSettingValue('mail_password', '****'),
         ];
       }
    
       $this->app->singleton('swift.transport', function ($app) { 
           return new Illuminate\Mail\TransportManager($app);
       });
    }
    

提交回复
热议问题