Skip to content

Commit

Permalink
Adding LaravelAdapter.
Browse files Browse the repository at this point in the history
  • Loading branch information
atheken committed Feb 26, 2015
1 parent 2afa0e6 commit 9d3c7c8
Show file tree
Hide file tree
Showing 3 changed files with 313 additions and 7 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"guzzlehttp/guzzle": "5.2"
},
"require-dev": {
"phpunit/phpunit": "~4.5"
"phpunit/phpunit": "~4.5",
"illuminate/mail": "~5.0"
},
"license": "MIT",
"autoload": {
Expand Down
228 changes: 222 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

89 changes: 89 additions & 0 deletions src/Postmark/Adapters/LaravelMailProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php namespace Postmark\Adapters;

use Illuminate\Mail\Mailer;
use Illuminate\Support\ServiceProvider;
use Swift_Mailer;

class LaravelMailProvider extends ServiceProvider {

protected $defer = true;

/**
* Register the service provider.
*
* @return void
*/
public function register() {
$this->app->singleton('mailer', function ($app) {
$this->registerSwiftMailer();

// 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['config']['mail.from'];

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

// Here we will determine if the mailer should be in "pretend" mode for this
// environment, which will simply write out e-mail to the logs instead of
// sending it over the web, which is useful for local dev environments.
$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('log')) {
$mailer->setLogger($app['log']->getMonolog());
}

if ($app->bound('queue')) {
$mailer->setQueue($app['queue.connection']);
}
}

/**
* Register the Swift Mailer instance.
*
* @return void
*/
public function registerSwiftMailer() {
$this->app['swift.mailer'] = $this->app->share(function ($app) {
$token = $this->app['config']->get('services.postmark');
return new Swift_Mailer(new \Postmark\Transport($token));
});
}

/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides() {
return ['mailer', 'swift.mailer'];
}

}

0 comments on commit 9d3c7c8

Please sign in to comment.