Enable Unique And Dynamic SMTP Mail Settings For Each User — Laravel 8+

Ikechukwu
2 min readOct 19, 2020
Laravel Framework

I have written a laravel package for this:

composer require ikechukwukalu/dynamicmailconfig

You can read more about it over the repo.

THE PROBLEM

You want to create a Laravel application where users are allowed to register and sign in on your application and you want to enable each user to send emails through your platform, using their own unique email address and password.

THE SOLUTION

  • A 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.
  • A 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
  • A Laravel MiddleWare: also create a middleware that would run when a user has been authenticated and register the ServiceProvider.

IMPLEMENTING THE MODEL

Create the migration file:

php artisan make:migration create_user_email_configurations_table
Schema::create('user_email_configurations', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained('users');
$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();
});
php artisan migrate

Create the Model class:

php artisan make:model UserEmailConfiguration
<?php
namespace App\Models;

use Illuminate\Support\Facades\Auth;
use Illuminate\Database\Eloquent\Model;

class userEmailConfiguration extends Model
{
protected $hidden = [
'name',
'address',
'driver',
'host',
'port',
'encryption',
'username',
'password'
];

public function scopeConfiguredEmail($query) {
return $query->where('user_id', Auth::user()->id);
}
}

IMPLEMENTING THE SERVICE PROVIDER

php artisan make:provider MailServiceProvider
<?php

namespace App\Providers;

use App\userEmailConfiguration;
use Illuminate\Support\ServiceProvider;

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

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('mail', 'Controller@test_mail')->middleware('mail');
});

That’s all. Enjoy !!!

--

--