Dynamic mail configuration with values from database [Laravel]

Viewed 20630

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

$settings = $cache->remember('settings', 60, function() use ($settings)
    {
        return $settings->pluck('value', 'name')->all();
    });

config()->set('settings', $settings);

settings table:

enter image description here

I am able to echo the value from the table like this:

{{ config('settings.sitename') }}  //returns Awesome Images

This works fine on any blade files or controllers in App\Http\Controllers

Problem:

I am trying to echo the value to App\config\mail.php like this:

'driver' => config('settings.maildriver'),
'host' => config('settings.mailhost'),

But I'm getting this error:

Missing argument 1 for Illuminate\Support\Manager::createDriver()

Update:

I have created a new service provider MailServiceProvider to override the settings in Mail.php like this:

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Config;

class MailServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        Config::set('mail.driver', config('settings.maildriver'));
        Config::set('mail.host', config('settings.mailhost'));
        Config::set('mail.port', config('settings.mailport'));
        Config::set('mail.encryption', config('settings.mailencryption'));
        Config::set('mail.username', config('settings.mailusername'));
        Config::set('mail.password', config('settings.mailpassword'));

    }
}

But still I am getting the same error!!

Is there any way to override default mail configuration (in app/config/mail.php) on-the-fly (e.g. configuration is stored in database) before swiftmailer transport is created?

4 Answers

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.

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.

After research a lot, finally I found the best possible way to dynamic mail configuration.

I am saving my mail configuration data in the settings table, please have a look at the table structure.

enter image description here

Helpers/AaapHelper.php

<?php

namespace App\Helpers;

use App\Setting;

class AppHelper
{

    public static function setMailConfig(){

        //Get the data from settings table
        $settings = Setting::pluck('description', 'label'); 

        //Set the data in an array variable from settings table
        $mailConfig = [
            'transport' => 'smtp',
            'host' => $settings['smtp_host'],
            'port' => $settings['smtp_port'],
            'encryption' => $settings['smtp_security'],
            'username' => $settings['smtp_username'],
            'password' => $settings['smtp_password'],
            'timeout' => null
        ];

        //To set configuration values at runtime, pass an array to the config helper
        config(['mail.mailers.smtp' => $mailConfig]);
    }
}

app\Http\Controllers\SettingController.php

<?php
namespace App\Http\Controllers;

use App\Helpers\AppHelper;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;

class SettingController extends Controller
{

    public function sendMail()
    {
        try
        {
            //Set mail configuration
            AppHelper::setMailConfig();

            $data = ['name' => "Virat Gandhi"];

            Mail::send(['text' => 'mail'], $data, function ($message)
            {
                $message->to('abc@gmail.com', 'Lorem Ipsum')
                    ->subject('Laravel Basic Testing Mail');
                $message->from('xyz@gmail.com', $data['name']);
            });
            return redirect()->back()->with('success', 'Test email sent successfully');
        }
        catch(\Exception $e)
        {
            return redirect()->back()->withErrors($e->getMessage());
        }
    }
}

Explanation

While sending a mail through the sendMail function it will first configure mail through helper.

Related