Dynamic mail configuration with values from database [Laravel]

后端 未结 3 1592
傲寒
傲寒 2021-02-01 07:09

I have created a service provider in my Laravel Application SettingsServiceProvider. This caches the settings table from the database.

$settings = $         


        
相关标签:
3条回答
  • 2021-02-01 07:49

    Following the instructions here is the proper solution to the problem, in case if you're sending multiple emails per request over different SMTP configurations, Config::set() won't work right, the first email will use the correct settings, while all upcoming emails within the same request will use the same configuration of the first one, because the Mail service is provided as a singleton thus only the initial configurations will be used.

    This also might affect emails sent over Laravel queue workers due to the same reason.

    0 讨论(0)
  • 2021-02-01 07:54

    Struggled for 3 days with this issue finally I figured out a way to solve it.

    First I created a table mails and populated it with my values. Then I created a provider MailConfigServiceProvider.php

    <?php
    
    namespace App\Providers;
    
    use Config;
    use Illuminate\Support\Facades\DB;
    use Illuminate\Support\ServiceProvider;
    
    class MailConfigServiceProvider extends ServiceProvider
    {
        /**
         * Bootstrap the application services.
         *
         * @return void
         */
        public function boot()
        {
            //
        }
    
        /**
         * Register the application services.
         *
         * @return void
         */
        public function register()
        {
            if (\Schema::hasTable('mails')) {
                $mail = DB::table('mails')->first();
                if ($mail) //checking if table is not empty
                {
                    $config = array(
                        'driver'     => $mail->driver,
                        'host'       => $mail->host,
                        'port'       => $mail->port,
                        'from'       => array('address' => $mail->from_address, 'name' => $mail->from_name),
                        'encryption' => $mail->encryption,
                        'username'   => $mail->username,
                        'password'   => $mail->password,
                        'sendmail'   => '/usr/sbin/sendmail -bs',
                        'pretend'    => false,
                    );
                    Config::set('mail', $config);
                }
            }
        }
    }
    

    And then registered it in the config\app.php

    App\Providers\MailConfigServiceProvider::class,
    
    0 讨论(0)
  • 2021-02-01 07:55

    Maybe its usefull to somebody, but I solved it as following;

    In a ServiceProvider under the boot-method;

    $settings = Cache::remember('settings', 60, function () {
        return Setting::pluck('value', 'name')->all();
    });
    
    config()->set('settings', $settings); // optional
    
    config()->set('mail', array_merge(config('mail'), [
        'driver' => 'mailgun',
        'from' => [
            'address' => $settings['mail_from_address'],
            'name' => $settings['mail_from_name']
        ]
    ]));
    
    config()->set('services', array_merge(config('services'), [
        'mailgun' => [
            'domain' => $settings['mailgun_domain'],
            'secret' => $settings['mailgun_secret']
        ]
    ]));
    

    I used array_merge with the original config, so we only override the values we need to. Also we can use the Cache-facade in the boot-method.

    0 讨论(0)
提交回复
热议问题