Forked from arthursalvtr/add-custom-mail-driver-to-laravel.md
Created
January 15, 2022 22:37
-
-
Save simtabi/b376beac3f98c61f2c00b6d7455be7fd to your computer and use it in GitHub Desktop.
Revisions
-
arthursalvtr revised this gist
Nov 26, 2019 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -37,7 +37,7 @@ it means we can easily extend the driver using this manager class! ## Create a MailDriver class In this case I am using OutlookMailDriver, and since i am going to use the extend method from the existing manager, it expects a Closure therefore i make an invocable method that return a function. ```php -
arthursalvtr revised this gist
Nov 26, 2019 . 1 changed file with 5 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,7 +5,6 @@ Tested on Laravel 5.8 Within the source code of laravel 5.8 at this time of writing, we have this following ```php <?php namespace Illuminate\Support; @@ -34,6 +33,7 @@ abstract class Manager ``` it means we can easily extend the driver using this manager class! ## Create a MailDriver class @@ -63,6 +63,7 @@ class OutlookMailDriver ``` >The signature of your invocable may differ from mine ## Create a CustomMailProvider that extend Illuminate\Mail\MailServiceProvider @@ -89,8 +90,10 @@ class CustomMailProvider extends MailServiceProvider protected function registerSwiftTransport() { parent::registerSwiftTransport(); $this->app->extend('swift.transport', function (TransportManager $transport) { $driver = 'outlook'; $callback = new OutlookMailDriver(); $transport->extend($driver, $callback($transport)); return $transport; }); -
arthursalvtr created this gist
Nov 26, 2019 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,159 @@ # How to add custom Mail Driver to Laravel Tested on Laravel 5.8 Within the source code of laravel 5.8 at this time of writing, we have this following ```php <?php namespace Illuminate\Support; // use... abstract class Manager { //somewhere in the body ... /** * Register a custom driver creator Closure. * * @param string $driver * @param \Closure $callback * @return $this */ public function extend($driver, Closure $callback) { $this->customCreators[$driver] = $callback; return $this; } // the rest of the code... } ``` ## Create a MailDriver class In this case I am using OutlookMailDriver, and since i am going to the extend method from the existing manager, it expects a Closure therefore i make an invocable method that return a function. ```php <?php declare(strict_types=1); namespace App\Helpers\Drivers; use App\Helpers\Transports\OutlookTransportManager; use Illuminate\Mail\TransportManager; class OutlookMailDriver { public function __invoke(TransportManager $manager) { return function ($app) { $config = $app['config']->get('services.outlook', []); return new OutlookTransportManager($config); }; } } ``` ## Create a CustomMailProvider that extend Illuminate\Mail\MailServiceProvider I am going to override the registerSwiftTransport method yet calling its parent first to have it registered its expected Manager then I extend it with my custom Mail Driver. ```php <?php namespace App\Providers; use App\Helpers\Drivers\OutlookMailDriver; use Illuminate\Mail\MailServiceProvider; use Illuminate\Mail\TransportManager; class CustomMailProvider extends MailServiceProvider { /** * Register the Swift Transport instance. * * @return void */ protected function registerSwiftTransport() { parent::registerSwiftTransport(); $this->app->extend('swift.transport', function (TransportManager $transport) { $transport->extend($driver, $callback($transport)); return $transport; }); } } ``` ## Create a class that extends Illuminate\Mail\Transport\Transport Since `Illuminate\Mail\Transport\Transport` is an abstract class therefore i need to have `send` method. ```php <?php declare(strict_types=1); namespace App\Helpers\Transports; use Illuminate\Mail\Transport\Transport; use Swift_Mime_SimpleMessage; class OutlookTransportManager extends Transport { public function __construct($config) { // do your whatever you need here } public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null) { logger('I am using custom outlook manager'); // TODO: Implement send() method. return 0; } } ``` ## Register the CustomMailProvider in your config/app.php comment Illuminate\Mail\MailServiceProvider::class within your config, and register your own custom provider there ```php return [ 'providers' => [ //... App\Providers\CustomMailProvider::class, //Illuminate\Mail\MailServiceProvider::class, // comment this out ], ]; ``` ## Test ```php // somewhere in your route/web.php Route::get('test', function () { Mail::to('[email protected]')->send(new \App\Mail\Test()); }); ```