Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
metalagman committed Jul 28, 2016
0 parents commit 06f0013
Show file tree
Hide file tree
Showing 5 changed files with 449 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# The MIT License (MIT)

Copyright (c) 2016 [YarCode Agency](http://yarcode.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# yarcode/yii2-async-mailer
Mailgun mailer implementation for Yii2

## Installation

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
php composer.phar require --prefer-dist yarcode/yii2-mailgun-mailer
```

or add

```json
"yarcode/yii2-mailgun-mailer": "*"
```

## Usage
Configure `YarCode\Yii2\Mailgun\Mailer` as your mailer.
```
'mailer' => [
'class' => '\YarCode\Yii2\Mailgun\Mailer',
'domain => 'example.org,
'apiKey => 'foobar',
],
```
Now you can send your emails as usual.
```
$message = \Yii::$app->mailer->compose()
->setSubject('test subject')
->setFrom('[email protected]')
->setHtmlBody('test body')
->setTo('[email protected]');
\Yii::$app->mailer->send($message);
```
36 changes: 36 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "yarcode/yii2-mailgun-mailer",
"description": "Mailgun mailer implementation for Yii2",
"type": "yii2-extension",
"keywords": [
"yii2",
"mailer",
"mailgun",
"email",
"mail",
"api"
],
"homepage": "http://packagist.org/packages/yarcode/yii2-mailgun-mailer",
"license": "MIT",
"authors": [
{
"name": "Alexey Samoylov",
"email": "[email protected]",
"role": "Developer"
}
],
"support": {
"issues": "https://github.com/yarcode/yii2-mailgun-mailer/issues",
"source": "https://github.com/yarcode/yii2-mailgun-mailer"
},
"require": {
"yiisoft/yii2": "~2",
"mailgun/mailgun-php": "^2.1",
"php-http/guzzle6-adapter": "^1.1"
},
"autoload": {
"psr-4": {
"YarCode\\Yii2\\Mailgun\\": "./src"
}
}
}
73 changes: 73 additions & 0 deletions src/Mailer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/**
* @author Alexey Samoylov <[email protected]>
*/
namespace YarCode\Yii2\Mailgun;

use Mailgun\Mailgun;
use yii\base\InvalidConfigException;
use yii\mail\BaseMailer;

class Mailer extends BaseMailer
{
/** @var string */
public $messageClass = Message::class;
/** @var string */
protected $apiKey;
/** @var string */
protected $domain;
/** @var Mailgun */
protected $client;

/**
* @param string $apiKey
*/
public function setApiKey($apiKey)
{
$this->apiKey = $apiKey;
}

/**
* @param string $domain
*/
public function setDomain($domain)
{
$this->domain = $domain;
}

/**
* @return Mailgun
*/
public function getClient()
{
if (empty($this->client)) {
$this->client = $this->createClient();
}
return $this->client;
}

/**
* @return Mailgun
* @throws InvalidConfigException
*/
protected function createClient()
{
if (empty($this->apiKey) || empty($this->domain)) {
throw new InvalidConfigException('Invalid mailer configuration');
}
return new Mailgun($this->apiKey);
}

/**
* @param Message $message
* @inheritdoc
*/
protected function sendMessage($message)
{
$this->getClient()->post("{$this->domain}/messages",
$message->getMessageBuilder()->getMessage(),
$message->getMessageBuilder()->getFiles()
);
return true;
}
}
Loading

0 comments on commit 06f0013

Please sign in to comment.