Last active
September 7, 2017 06:17
-
-
Save rummykhan/be3dd904cc85d38f411315a1d62a7dd1 to your computer and use it in GitHub Desktop.
This file contains 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 characters
1. Copy MandrillServiceProvider in app/Providers/ directory. | |
2. Copy Mandrill in app/Facades/ directory | |
3. add MandrillServiceProvider in providers array | |
'providers' => [ | |
\App\Providers\MandrillServiceProvider::class, | |
] | |
add Mandrill in facades array | |
'facades' => [ | |
'Mandrill' => App\Facades\Mandrill::class, | |
] | |
Update mandrill config in services.php | |
'mandrill' => [ | |
'secret' => 'aquickbrownfoxjumpsoverthelazydog', | |
], | |
Now you've two mail clients, Mailer and Mandrill, we can use any of the two. | |
e.g. | |
using Facades | |
\Illuminate\Support\Facades\Mail::send('mail', [], function ($mail) { | |
$mail->to('[email protected]'); | |
}); | |
\App\Facades\Mandrill::send('mail', [], function ($mail) { | |
$mail->to('[email protected]'); | |
}); | |
using app | |
app()->make('mailer')->send('mail',[], function ($mail) { | |
$mail->to('[email protected]'); | |
}); | |
app()->make('mandrill')->send('mail',[], function ($mail) { | |
$mail->to('[email protected]'); | |
}); | |
Thanks. | |
[email protected] |
This file contains 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 characters
<?php | |
namespace App\Facades; | |
use Illuminate\Support\Facades\Facade; | |
class Mandrill extends Facade | |
{ | |
/** | |
* Get the registered name of the component. | |
* | |
* @return string | |
*/ | |
protected static function getFacadeAccessor() | |
{ | |
return 'mandrill'; | |
} | |
} |
This file contains 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 characters
<?php | |
namespace App\Providers; | |
use Illuminate\Mail\Mailer; | |
use Illuminate\Mail\TransportManager; | |
use Illuminate\Support\Arr; | |
use Illuminate\Support\ServiceProvider; | |
use Illuminate\Support\Str; | |
use Swift_Mailer; | |
class MandrillServiceProvider extends ServiceProvider | |
{ | |
/** | |
* Indicates if loading of the provider is deferred. | |
* | |
* @var bool | |
*/ | |
protected $defer = true; | |
/** | |
* Register the service provider. | |
* | |
* @return void | |
*/ | |
public function register() | |
{ | |
$this->registerSwiftMailer(); | |
$this->registerIlluminateMailer(); | |
} | |
/** | |
* Register the Illuminate mailer instance. | |
* | |
* @return void | |
*/ | |
protected function registerIlluminateMailer() | |
{ | |
$this->app->singleton('mandrill', function ($app) { | |
$config = $app->make('config')->get('mail'); | |
// Once we have create the mailer instance, we will set a container instance | |
// on the mailer. This allows us to resolve mailer classes via containers | |
// for maximum testability on said classes instead of passing Closures. | |
$mailer = new Mailer( | |
$app['view'], $app['swift.mandrill'], $app['events'] | |
); | |
if ($app->bound('queue')) { | |
$mailer->setQueue($app['queue']); | |
} | |
// Next we will set all of the global addresses on this mailer, which allows | |
// for easy unification of all "from" addresses as well as easy debugging | |
// of sent messages since they get be sent into a single email address. | |
foreach (['from', 'reply_to', 'to'] as $type) { | |
$this->setGlobalAddress($mailer, $config, $type); | |
} | |
return $mailer; | |
}); | |
} | |
/** | |
* Set a global address on the mailer by type. | |
* | |
* @param \Illuminate\Mail\Mailer $mailer | |
* @param array $config | |
* @param string $type | |
* @return void | |
*/ | |
protected function setGlobalAddress($mailer, array $config, $type) | |
{ | |
$address = Arr::get($config, $type); | |
if (is_array($address) && isset($address['address'])) { | |
$mailer->{'always'.Str::studly($type)}($address['address'], $address['name']); | |
} | |
} | |
/** | |
* Register the Swift Mailer instance. | |
* | |
* @return void | |
*/ | |
public function registerSwiftMailer() | |
{ | |
// Once we have the transporter registered, we will register the actual Swift | |
// mailer instance, passing in the transport instances, which allows us to | |
// override this transporter instances during app start-up if necessary. | |
$this->app->singleton('swift.mandrill', function ($app) { | |
return new Swift_Mailer($app['swift.transport']->driver('mandrill')); | |
}); | |
} | |
/** | |
* Get the services provided by the provider. | |
* | |
* @return array | |
*/ | |
public function provides() | |
{ | |
return [ | |
'mandrill', 'swift.mandrill' | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment