Skip to content

Commit

Permalink
Init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitaly Baev committed Sep 9, 2016
0 parents commit 6b1253e
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor
composer.lock
.idea
14 changes: 14 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "vitalybaev/laravel5-dkim",
"description": "Laravel 5 package for signing outgoing messages with DKIM.",
"license": "MIT",
"authors": [
{
"name": "Vitaly Baev",
"email": "[email protected]"
}
],
"require": {
"laravel/framework": "^5.2"
}
}
46 changes: 46 additions & 0 deletions src/DkimMailServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Vitalybaev\LaravelDkim;

use Illuminate\Mail\MailServiceProvider;

class DkimMailServiceProvider extends MailServiceProvider
{
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
parent::registerSwiftMailer();

$this->app->singleton('mailer', function ($app) {
// 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']);
}

$to = $app['config']['mail.to'];

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

return $mailer;
});
}
}
29 changes: 29 additions & 0 deletions src/Mailer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Vitalybaev\LaravelDkim;

use Swift_Message;

class Mailer extends \Illuminate\Mail\Mailer
{
protected function createMessage()
{
$message = new Message(new Swift_Message);

// If a global from address has been specified we will set it on every message
// instances so the developer does not have to repeat themselves every time
// they create a new message. We will just go ahead and push the address.
if (! empty($this->from['address'])) {
$message->from($this->from['address'], $this->from['name']);
}

if (config('mail.driver') == 'smtp') {
if (config('mail.dkim_selector') && config('mail.dkim_domain') && config('mail.dkim_private_key')) {
$message->attachDkim(config('mail.dkim_selector'), config('mail.dkim_domain'), config('mail.dkim_private_key'));
}
}

return $message;
}

}
23 changes: 23 additions & 0 deletions src/Message.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Vitalybaev\LaravelDkim;

use Swift_Signers_DKIMSigner;

class Message extends \Illuminate\Mail\Message
{
/**
* @param $selector
* @param $domain
* @param $privateKey
*
* @return $this
*/
public function attachDkim($selector, $domain, $privateKey)
{
$signer = new Swift_Signers_DKIMSigner($privateKey, $domain, $selector);
$this->swift->attachSigner($signer);

return $this;
}
}

0 comments on commit 6b1253e

Please sign in to comment.