Basically laravel provide us Mail Configuration setup with the mail pre-build functionality. But many times we need to add Dynamic Mail Configuration with Values in Laravel because if the user want to getting mail with different mail drivers then he can add your mail drivers details in admin panel and save the database then we can get the users details and send then their latest added mail drivers credentials. We can override the laravel mail functionality and use custom dynamic mail configuration easy way.
Sending mail in any user very important for admin panel, in laravel send email is very easy default we use mailtrap but we can send any other mail drivers. So let’s start for how to create and use Dynamic Mail Configuration in Laravel step by step for users added mail drivers credential added in database table values.
Step 1: Create a Custom Mail Provider
First create a table email_settings
and add it with user mail credentials values. Then create a provider MailConfigServiceProvider.php
just running the following command.
php artisan make:provider MailConfigServiceProvider
After creating the service provider not setup the users credentials dynamically and override the laravel mail functionality.
app\Providers\MailConfigServiceProvider.php
class MailConfigServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
$emailServices = EmailServices::where(['active' => 1])->latest()->first();
if ($emailServices) {
$config = array(
'driver' => $emailServices->driver,
'host' => $emailServices->host,
'port' => $emailServices->port,
'username' => $emailServices->username,
'password' => $emailServices->password,
'encryption' => null,
'from' => array('address' => 'admin@example.com', 'name' => $emailServices->name),
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
);
Config::set('mail', $config);
}
}
}
You need to make a email_services table in database and crud for the user whom they add their mail credentials and get the email which they added in last time.
Step 2: Add Service Provider
Now register your service provider in the config\app.php
file.
/*
|--------------------------------------------------------------------------
| Autoloaded Service Providers
|--------------------------------------------------------------------------
|
| The service providers listed here will be automatically loaded on the
| request to your application. Feel free to add your own services to
| this array to grant expanded functionality to your applications.
|
*/
'providers' => [
App\Providers\MailConfigServiceProvider::class,
],
After saving that remove the mail credentials from .env file and you can check dynamic mail functionality just sending the forgot password function and you check you get the mail which is the user added in their email_services table.
not working
Its perfect code i have added in project too
how do i access mail username and password in controller ????