8000 [Proposal] De-couple mail component a bit more by driesvints · Pull Request #5032 · laravel/framework · GitHub
[go: up one dir, main page]

Skip to content

[Proposal] De-couple mail component a bit more #5032

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 10, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@
}
},
"suggest": {
"doctrine/dbal": "Allow renaming columns and dropping SQLite columns."
"doctrine/dbal": "Allow renaming columns and dropping SQLite columns.",
"guzzlehttp/guzzle": "Required for Mailgun and Mandrill mail drivers."
},
"minimum-stability": "dev"
}
150 changes: 5 additions & 145 deletions src/Illuminate/Mail/MailServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@

use Swift_Mailer;
use Illuminate\Support\ServiceProvider;
use Swift_SmtpTransport as SmtpTransport;
use Swift_MailTransport as MailTransport;
use Illuminate\Mail\Transport\LogTransport;
use Illuminate\Mail\Transport\MailgunTransport;
use Illuminate\Mail\Transport\MandrillTransport;
use Swift_SendmailTransport as SendmailTransport;

class MailServiceProvider extends ServiceProvider {

Expand Down Expand Up @@ -90,161 +84,27 @@ protected function setMailerDependencies($mailer, $app)
*/
public function registerSwiftMailer()
{
$config = $this->app['config']['mail'];

$this->registerSwiftTransport($config);
$this->registerSwiftTransport();

// 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['swift.mailer'] = $this->app->share(function($app)
{
return new Swift_Mailer($app['swift.transport']);
return new Swift_Mailer($app['swift.transport']->driver());
});
}

/**
* Register the Swift Transport instance.
*
* @param array $config
* @return void
*
* @throws \InvalidArgumentException
*/
protected function registerSwiftTransport($config)
{
switch ($config['driver'])
{
case 'smtp':
return $this->registerSmtpTransport($config);

case 'sendmail':
return $this->registerSendmailTransport($config);

case 'mail':
return $this->registerMailTransport($config);

case 'mailgun':
return $this->registerMailgunTransport($config);

case 'mandrill':
return $this->registerMandrillTransport($config);

case 'log':
return $this->registerLogTransport($config);

default:
throw new \InvalidArgumentException('Invalid mail driver.');
}
}

/**
* Register the SMTP Swift Transport instance.
*
* @param array $config
* @return void
*/
protected function registerSmtpTransport($config)
{
$this->app['swift.transport'] = $this->app->share(function() use ($config)
{
extract($config);

// The Swift SMTP transport instance will allow us to use any SMTP backend
// for delivering mail such as Sendgrid, Amazon SES, or a custom server
// a developer has available. We will just pass this configured host.
$transport = SmtpTransport::newInstance($host, $port);

if (isset($encryption))
{
$transport->setEncryption($encryption);
}

// Once we have the transport we will check for the presence of a username
// and password. If we have it we will set the credentials on the Swift
// transporter instance so that we'll properly authenticate delivery.
if (isset($username))
{
$transport->setUsername($username);

$transport->setPassword($password);
}

return $transport;
});
}

/**
* Register the Sendmail Swift Transport instance.
*
* @param array $config
* @return void
*/
protected function registerSendmailTransport($config)
{
$this->app['swift.transport'] = $this->app->share(function() use ($config)
{
return SendmailTransport::newInstance($config['sendmail']);
});
}

/**
* Register the Mail Swift Transport instance.
*
* @param array $config
* @return void
*/
protected function registerMailTransport($config)
{
$this->app['swift.transport'] = $this->app->share(function()
{
return MailTransport::newInstance();
});
}

/**
* Register the Mailgun Swift Transport instance.
*
* @param array $config
* @return void
*/
protected function registerMailgunTransport($config)
{
$mailgun = $this->app['config']->get('services.mailgun', array());

$this->app->bindShared('swift.transport', function() use ($mailgun)
{
return new MailgunTransport($mailgun['secret'], $mailgun['domain']);
});
}

/**
* Register the Mandrill Swift Transport instance.
*
* @param array $config
* @return void
*/
protected function registerMandrillTransport($config)
{
$mandrill = $this->app['config']->get('services.mandrill', array());

$this->app->bindShared('swift.transport', function() use ($mandrill)
{
return new MandrillTransport($mandrill['secret']);
});
}

/**
* Register the "Log" Swift Transport instance.
*
* @param array $config
* @return void
*/
protected function registerLogTransport($config)
protected function registerSwiftTransport()
{
$this->app->bindShared('swift.transport', function($app)
$this->app['swift.transport'] = $this->app->share(function($app)
{
return new LogTransport($app['log']->getMonolog());
return new TransportManager($app);
});
}

Expand Down
1 change: 0 additions & 1 deletion src/Illuminate/Mail/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use Closure;
use Swift_Mailer;
use Swift_Message;
use Illuminate\Log\Writer;
use Psr\Log\LoggerInterface;
use Illuminate\Container\Container;
use Illuminate\Contracts\View\Factory;
Expand Down
122 changes: 122 additions & 0 deletions src/Illuminate/Mail/TransportManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php namespace Illuminate\Mail;

use Illuminate\Support\Manager;
use Swift_SmtpTransport as SmtpTransport;
use Swift_MailTransport as MailTransport;
use Illuminate\Mail\Transport\LogTransport;
use Illuminate\Mail\Transport\MailgunTransport;
use Illuminate\Mail\Transport\MandrillTransport;
use Swift_SendmailTransport as SendmailTransport;

class TransportManager extends Manager {

/**
* Create an instance of the SMTP Swift Transport driver.
*
* @return \Swift_SmtpTransport
*/
protected function createSmtpDriver()
{
$config = $this->app['config']['mail'];

// The Swift SMTP transport instance will allow us to use any SMTP backend
// for delivering mail such as Sendgrid, Amazon SES, or a custom server
// a developer has available. We will just pass this configured host.
$transport = SmtpTransport::newInstance($config['host'], $config['port']);

if (isset($config['encryption']))
{
$transport->setEncryption($config['encryption']);
}

// Once we have the transport we will check for the presence of a username
// and password. If we have it we will set the credentials on the Swift
// transporter instance so that we'll properly authenticate delivery.
if (isset($config['username']))
{
$transport->setUsername($config['username']);

$transport->setPassword($config['password']);
}

return $transport;
}

/**
* Create an instance of the Sendmail Swift Transport driver.
*
* @return \Swift_SendmailTransport
*/
protected function createSendmailDriver()
{
$command = $this->app['config']['mail']['sendmail'];

return SendmailTransport::newInstance($command);
}

/**
* Create an instance of the Mail Swift Transport driver.
*
* @return \Swift_MailTransport
*/
protected function createMailDriver()
{
return MailTransport::newInstance();
}

/**
* Create an instance of the Mailgun Swift Transport driver.
*
* @return \Illuminate\Mail\Transport\MailgunTransport
*/
protected function createMailgunDriver()
{
$config = $this->app['config']->get('services.mailgun', array());

return new MailgunTransport($config['secret'], $config['domain']);
}

/**
* Create an instance of the Mandrill Swift Transport driver.
*
* @return \Illuminate\Mail\Transport\MandrillTransport
*/
protected function createMandrillDriver()
{
$config = $this->app['config']->get('services.mandrill', array());

return new MandrillTransport($config['secret']);
}

/**
* Create an instance of the Log Swift Transport driver.
*
* @return \Illuminate\Mail\Transport\LogTransport
*/
protected function createLogDriver()
{
return new LogTransport($this->app['log']->getMonolog());
}

/**
* Get the default cache driver name.
*
* @return string
*/
public function getDefaultDriver()
{
return $this->app['config']['mail.driver'];
}

/**
* Set the default cache driver name.
*
* @param string $name
* @return void
*/
public function setDefaultDriver($name)
{
$this->app['config']['mail.driver'] = $name;
}

}
3 changes: 3 additions & 0 deletions src/Illuminate/Mail/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,8 @@
"dev-master": "4.3-dev"
}
},
"suggest": {
"guzzlehttp/guzzle": "Required for Mailgun and Mandrill mail drivers."
},
"minimum-stability": "dev"
}
0