<?php
namespace Illuminate\Mail;
use Swift_Mailer;
use Illuminate\Support\ServiceProvider;
class MailServiceProvider 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->app->singleton('mailer', function ($app) {
$mailer = new Mailer(
$app['view'], $app['swift.mailer'], $app['events']
);
$this->setMailerDependencies($mailer, $app);
$from = $app['config']['mail.from'];
if (is_array($from) && isset($from['address'])) {
$mailer->alwaysFrom($from['address'], $from['name']);
}
$to = $app['config']['mail.to'];
if (is_array($to) && isset($to['address'])) {
$mailer->alwaysTo($to['address'], $to['name']);
}
$pretend = $app['config']->get('mail.pretend', false);
$mailer->pretend($pretend);
return $mailer;
});
}
* Set a few dependencies on the mailer instance.
*
* @param \Illuminate\Mail\Mailer $mailer
* @param \Illuminate\Foundation\Application $app
* @return void
*/
protected function setMailerDependencies($mailer, $app)
{
$mailer->setContainer($app);
if ($app->bound('Psr\Log\LoggerInterface')) {
$mailer->setLogger($app->make('Psr\Log\LoggerInterface'));
}
if ($app->bound('queue')) {
$mailer->setQueue($app['queue.connection']);
}
}
* Register the Swift Mailer instance.
*
* @return void
*/
public function registerSwiftMailer()
{
$this->registerSwiftTransport();
$this->app['swift.mailer'] = $this->app->share(function ($app) {
return new Swift_Mailer($app['swift.transport']->driver());
});
}
* Register the Swift Transport instance.
*
* @return void
*/
protected function registerSwiftTransport()
{
$this->app['swift.transport'] = $this->app->share(function ($app) {
return new TransportManager($app);
});
}
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return ['mailer', 'swift.mailer', 'swift.transport'];
}
}