If you want to create a Laravel 7
application where users are allowed to register and sign in on your application and you intend to enable each user with the ability to send emails through your platform, using their own unique email address and password.
THE SOLUTION:
- Laravel Model: first you’ll need to create a database table to store the user’s email configuration data. Next, you’ll need an Eloquent Model to retrieve an authenticated user’s id to fetch their email configuration data dynamically.
- Laravel ServiceProvider: next, create a service provider that would query the database for the user’s email configurations using a scope method within your Model class and would set it as their default mail configuration. Do not register this service provider within your
config/app.php
- Laravel MiddleWare: also create a middleware that would run when a user has been authenticated and register the ServiceProvider.
IMPLEMENTING THE MODEL:
Make a migration. Run these from the command line php artisan make:migration create_user_email_configurations_table
. Then:
Schema::create('user_email_configurations', function (Blueprint $table) {
$table->id();
$table->string('user_id');
$table->string('name');
$table->string('address');
$table->string('driver');
$table->string('host');
$table->string('port');
$table->string('encryption');
$table->string('username');
$table->string('password');
$table->timestamps();
});
Finalize and create your model. Runphp artisan migrate
and
php artisan make:model userEmailConfiguration
. Now add a scope method into your model.
<?php
namespace App;
use Illuminate\Support\Facades\Auth;
use Illuminate\Database\Eloquent\Model;
class userEmailConfiguration extends Model
{
protected $hidden = [
'driver',
'host',
'port',
'encryption',
'username',
'password'
];
public function scopeConfiguredEmail($query) {
$user = Auth::user();
return $query->where('user_id', $user->id);
}
}
IMPLEMENTING THE SERVICEPROVIDER
Run this from the command line - php artisan make:provider MailServiceProvider
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\userEmailConfiguration;
use Config;
class MailServiceProvider extends ServiceProvider
{
public function register()
{
$mail = userEmailConfiguration::configuredEmail()->first();
if (isset($mail->id))
{
$config = array(
'driver' => $mail->driver,
'host' => $mail->host,
'port' => $mail->port,
'from' => array('address' => $mail->address, 'name' => $mail->name),
'encryption' => $mail->encryption,
'username' => $mail->username,
'password' => $mail->password
);
Config::set('mail', $config);
}
}
public function boot()
{
}
}
IMPLEMENTING THE MIDDLEWARE
Run the following command - php artisan make:middleware MailService
<?php
namespace App\Http\Middleware;
use Closure;
use App;
class MailService
{
public function handle($request, Closure $next)
{
$app = App::getInstance();
$app->register('App\Providers\MailServiceProvider');
return $next($request);
}
}
Now that we’ve implemented all that, register your middleware within your $routedMiddleware
array in your kennel.php
as mail
. Then call it up within your authenticated routes middleware:
Sample:
Route::group(['middleware' => [ 'auth:api' ]], function () {
Route::post('send/email', 'Controller@test_mail')->middleware('mail');
});
Here's my original post over medium -
Enable Unique And Dynamic SMTP Mail Settings For Each User — Laravel 7