Skip to content

Commit

Permalink
Finish cleaning up mail component.
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Dec 20, 2016
1 parent 44958f0 commit 5dace8f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 95 deletions.
49 changes: 19 additions & 30 deletions src/Illuminate/Mail/MailServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Illuminate\Mail;

use Swift_Mailer;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Support\ServiceProvider;

class MailServiceProvider extends ServiceProvider
Expand Down Expand Up @@ -36,57 +38,44 @@ public function register()
protected function registerIlluminateMailer()
{
$this->app->singleton('mailer', 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.mailer'], $app['events']
);

$this->setMailerDependencies($mailer, $app);

// If a "from" address is set, we will set it on the mailer so that all mail
// messages sent by the applications will utilize the same "from" address
// on each one, which makes the developer's life a lot more convenient.
$from = $app->make('config')->get('mail.from');

if (is_array($from) && isset($from['address'])) {
$mailer->alwaysFrom($from['address'], $from['name']);
}

// If a "to" address is set we will set all mail messages to be delivered to
// this single address. This is primarily for easier viewing of emails if
// the application developer is still experimenting with the emails UI.
$to = $app->make('config')->get('mail.to');

if (is_array($to) && isset($to['address'])) {
$mailer->alwaysTo($to['address'], $to['name']);
if ($app->bound('queue')) {
$mailer->setQueue($app['queue']);
}

// If a "reply to" address is set, we will set it on the mailer so that each
// message sent by the application will utilize the same address for this
// setting. This is more convenient than specifying it on each message.
$replyTo = $app['config']['mail.reply_to'];

if (is_array($replyTo) && isset($replyTo['address'])) {
$mailer->alwaysReplyTo($replyTo['address'], $replyTo['name']);
// 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 few dependencies on the mailer instance.
* Set a global address on the mailer by type.
*
* @param \Illuminate\Mail\Mailer $mailer
* @param \Illuminate\Foundation\Application $app
* @param array $config
* @param string $type
* @return void
*/
protected function setMailerDependencies($mailer, $app)
protected function setGlobalAddress($mailer, array $config, $type)
{
if ($app->bound('queue')) {
$mailer->setQueue($app['queue']);
$address = Arr::get($config, $type);

if (is_array($address) && isset($address['address'])) {
$mailer->{'always'.Str::studly($type)}($address['address'], $address['name']);
}
}

Expand Down
53 changes: 6 additions & 47 deletions src/Illuminate/Mail/Transport/SparkPostTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,6 @@ class SparkPostTransport extends Transport
*/
protected $options = [];

/**
* Transmission metadata.
*
* @var array
*/
protected $metadata = [];

/**
* Create a new SparkPost transport instance.
*
Expand All @@ -44,12 +37,11 @@ class SparkPostTransport extends Transport
* @param array $metadata
* @return void
*/
public function __construct(ClientInterface $client, $key, $options = [], $metadata = [])
public function __construct(ClientInterface $client, $key, $options = [])
{
$this->key = $key;
$this->client = $client;
$this->options = $options;
$this->metadata = $metadata;
}

/**
Expand All @@ -59,31 +51,19 @@ public function send(Swift_Mime_Message $message, &$failedRecipients = null)
{
$this->beforeSendPerformed($message);

$recipients = $this->getRecipients($message);

$message->setBcc([]);

$options = [
$this->client->post('https://api.sparkpost.com/api/v1/transmissions', [
'headers' => [
'Authorization' => $this->key,
],
'json' => [
'recipients' => $recipients,
'json' => array_merge([
'recipients' => $this->getRecipients($message),
'content' => [
'email_rfc822' => $message->toString(),
],
],
];

if ($this->options) {
$options['json']['options'] = $this->options;
}

if ($this->metadata) {
$options['json']['metadata'] = $this->metadata;
}

$this->client->post('https://api.sparkpost.com/api/v1/transmissions', $options);
], $this->options),
]);

$this->sendPerformed($message);

Expand Down Expand Up @@ -162,25 +142,4 @@ public function setOptions(array $options)
{
return $this->options = $options;
}

/**
* Get the transmission metadata being used by the transport.
*
* @return string
*/
public function getMetadata()
{
return $this->metadata;
}

/**
* Set the transmission metadata being used by the transport.
*
* @param array $metadata
* @return array
*/
public function setMetadata(array $metadata)
{
return $this->metadata = $metadata;
}
}
48 changes: 30 additions & 18 deletions src/Illuminate/Mail/TransportManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class TransportManager extends Manager
*/
protected function createSmtpDriver()
{
$config = $this->app['config']['mail'];
$config = $this->app->make('config')->get('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
Expand All @@ -46,6 +46,9 @@ protected function createSmtpDriver()
$transport->setPassword($config['password']);
}

// Next we will set any stream context options specified for the transport
// and then return it. The option is not required any may not be inside
// the configuration array at all so we'll verify that before adding.
if (isset($config['stream'])) {
$transport->setStreamOptions($config['stream']);
}
Expand All @@ -60,9 +63,9 @@ protected function createSmtpDriver()
*/
protected function createSendmailDriver()
{
$command = $this->app['config']['mail']['sendmail'];

return SendmailTransport::newInstance($command);
return SendmailTransport::newInstance(
$this->app['config']['mail']['sendmail']
);
}

/**
Expand All @@ -72,17 +75,28 @@ protected function createSendmailDriver()
*/
protected function createSesDriver()
{
$config = $this->app['config']->get('services.ses', []);

$config += [
$config = array_merge($this->app['config']->get('services.ses', []), [
'version' => 'latest', 'service' => 'email',
];
]);

return new SesTransport(new SesClient(
$this->addSesCredentials($config)
));
}

/**
* Add the SES credentials to the configuration array.
*
* @param array $config
* @return array
*/
protected function addSesCredentials(array $config)
{
if ($config['key'] && $config['secret']) {
$config['credentials'] = Arr::only($config, ['key', 'secret']);
}

return new SesTransport(new SesClient($config));
return $config;
}

/**
Expand All @@ -105,7 +119,7 @@ protected function createMailgunDriver()
$config = $this->app['config']->get('services.mailgun', []);

return new MailgunTransport(
$this->getHttpClient($config),
$this->guzzle($config),
$config['secret'], $config['domain']
);
}
Expand All @@ -120,7 +134,7 @@ protected function createMandrillDriver()
$config = $this->app['config']->get('services.mandrill', []);

return new MandrillTransport(
$this->getHttpClient($config), $config['secret']
$this->guzzle($config), $config['secret']
);
}

Expand All @@ -134,9 +148,7 @@ protected function createSparkPostDriver()
$config = $this->app['config']->get('services.sparkpost', []);

return new SparkPostTransport(
$this->getHttpClient($config),
$config['secret'],
Arr::get($config, 'options', [])
$this->guzzle($config), $config['secret'], Arr::get($config, 'options', [])
);
}

Expand All @@ -156,11 +168,11 @@ protected function createLogDriver()
* @param array $config
* @return \GuzzleHttp\Client
*/
protected function getHttpClient($config)
protected function guzzle($config)
{
$guzzleConfig = Arr::get($config, 'guzzle', []);

return new HttpClient(Arr::add($guzzleConfig, 'connect_timeout', 60));
return new HttpClient(Arr::add(
Arr::get($config, 'guzzle', []), 'connect_timeout', 60
));
}

/**
Expand Down

0 comments on commit 5dace8f

Please sign in to comment.