Skip to content

Instantly share code, notes, and snippets.

@arif98741
Last active December 11, 2022 09:19
Show Gist options
  • Save arif98741/31669afa4cec51446f17eca1512139b5 to your computer and use it in GitHub Desktop.
Save arif98741/31669afa4cec51446f17eca1512139b5 to your computer and use it in GitHub Desktop.
Multiple SMTP Configuration for sending mail . The mail purpose of using this is use to send mail using individual smtp for individual user
<?php
namespace App\Providers;
use Config;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\ServiceProvider;
class MailConfigServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
if (\Schema::hasTable('mails')) {
//make this provider and save it inside config/app.php as a provider. Run php artisan optimize then
//compose all the views....
view()->composer('*', function ($view) {
$mail = DB::table('mails')->where('user_id', auth()->user()->id)->offset(1)->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);
}
});
}
}
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('mails', function (Blueprint $table) {
$table->id();
$table->string('driver')->nullable();
$table->string('host')->nullable();
$table->string('port')->nullable();
$table->string('from_address')->nullable();
$table->string('from_name')->nullable();
$table->string('encryption')->nullable();
$table->string('username')->nullable();
$table->string('password')->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('mails');
}
};
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class MyTestMail extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->subject('Send mail subject')
->view('emails.myTestMail');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment