This commit is contained in:
Maxime Renou 2020-05-12 17:16:40 +02:00
parent f0a31cb9e8
commit 039beded65
8 changed files with 40 additions and 36 deletions

View File

@ -1,4 +1,4 @@
# laravel-bmail # laravel-mail
The Bluescale Mail package allows you to send Bluescale Mail templates using Laravel notifications system. The Bluescale Mail package allows you to send Bluescale Mail templates using Laravel notifications system.
@ -10,10 +10,7 @@ First in your composer.json, add:
"require": { "require": {
"bluescale/laravel-bmail": "dev-master" "bluescale/laravel-bmail": "dev-master"
} }
```
```
"repositories": [ "repositories": [
{ {
"type": "vcs", "type": "vcs",
@ -22,13 +19,13 @@ First in your composer.json, add:
] ]
``` ```
Next, update your package: Next, install the package:
```bash ```bash
composer update bluescale/laravel-bmail composer update bluescale/laravel-bmail
``` ```
Eventually, if you want to customize the config system: If you want to customize the config system:
```bash ```bash
php artisan vendor:publish php artisan vendor:publish
@ -54,10 +51,10 @@ First, create a new notification or use an existing one:
php artisan make:notification MyAwesomeNotification php artisan make:notification MyAwesomeNotification
``` ```
Add BMailChannel in the `via()` method: Add BluescaleMailChannel in the `via()` method:
```php ```php
return [BMailChannel::class]; return [BluescaleMailChannel::class];
``` ```
Then, create a 'toTemplate()' method, in which you specify your template fields : Then, create a 'toTemplate()' method, in which you specify your template fields :
@ -65,7 +62,7 @@ Then, create a 'toTemplate()' method, in which you specify your template fields
```php ```php
public function toTemplate($notifiable) public function toTemplate($notifiable)
{ {
return (new BMailTemplate("my-template-id")) return (new BluescaleMailTemplate("my-template-id"))
->sender("john.doe@gmail.com", "John OPTIONAL") // OPTIONAL ->sender("john.doe@gmail.com", "John OPTIONAL") // OPTIONAL
->replyTo("jean.grey@gmail.com", "Jean OPTIONAL") // OPTIONAL ->replyTo("jean.grey@gmail.com", "Jean OPTIONAL") // OPTIONAL
->recipients([ ->recipients([

View File

@ -1,14 +1,16 @@
{ {
"name": "bluescale/laravel-bmail", "name": "bluescale/laravel-mail",
"description": "Send email templates on Laravel to the Bluescale API.", "description": "Send Bluescale Mail templates from your Laravel notifications.",
"keywords": [ "keywords": [
"package", "package",
"bluescale", "bluescale",
"bmail",
"mail",
"api", "api",
"template", "template",
"email" "email"
], ],
"homepage": "https://git.bluesquare.io/bluescale/laravel-bmail", "homepage": "https://git.bluesquare.io/bluescale/laravel-mail",
"license": "proprietary", "license": "proprietary",
"authors": [ "authors": [
{ {
@ -21,13 +23,13 @@
"minimum-stability": "dev", "minimum-stability": "dev",
"autoload": { "autoload": {
"psr-4": { "psr-4": {
"BMail\\": "src/" "Bluescale\\Mail\\": "src/"
} }
}, },
"extra": { "extra": {
"laravel": { "laravel": {
"providers": [ "providers": [
"BMail\\BMailServiceProvider" "Bluescale\\Mail\\BluescaleMailServiceProvider"
] ]
} }
}, },

View File

@ -1,5 +0,0 @@
<?php
namespace BMail;
class BMailException extends \Exception {}

View File

@ -1,11 +1,12 @@
<?php <?php
namespace BMail; namespace Bluescale\Mail;
use Bluescale\Mail\BluescaleMailTemplate;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use Psr\Http\Message\StreamInterface; use Psr\Http\Message\StreamInterface;
class BMailApi class BluescaleMailApi
{ {
/** /**
* The API key * The API key
@ -20,7 +21,7 @@ class BMailApi
} }
/** /**
* @param $template * @param BluescaleMailTemplate $template
* @param $notifiable * @param $notifiable
* @return \Psr\Http\Message\StreamInterface * @return \Psr\Http\Message\StreamInterface
* @throws BMailException * @throws BMailException
@ -57,7 +58,7 @@ class BMailApi
])->getBody(); ])->getBody();
} catch(\Exception $e) { } catch(\Exception $e) {
throw new BMailException($e->getMessage()); throw new BluescaleMailException($e->getMessage());
} }
} }
} }

View File

@ -1,11 +1,11 @@
<?php <?php
namespace BMail; namespace Bluescale\Mail;
use Illuminate\Notifications\Notification; use Illuminate\Notifications\Notification;
use Psr\Http\Message\StreamInterface; use Psr\Http\Message\StreamInterface;
class BMailChannel class BluescaleMailChannel
{ {
/** /**
* @param $notifiable * @param $notifiable
@ -13,10 +13,10 @@ class BMailChannel
* @return \Psr\Http\Message\StreamInterface * @return \Psr\Http\Message\StreamInterface
* @throws BMailException * @throws BMailException
*/ */
public function send($notifiable, Notification $notification): StreamInterface public function send($notifiable, Notification $notification, BluescaleMailApi $api): StreamInterface
{ {
$template = $notification->toTemplate($notifiable); $template = $notification->toTemplate($notifiable);
return (new BMailApi(config("bmail.api_key")))->send($template, $notifiable); return $api->send($template, $notifiable);
} }
} }

View File

@ -0,0 +1,5 @@
<?php
namespace Bluescale\Mail;
class BluescaleMailException extends \Exception {}

View File

@ -1,10 +1,10 @@
<?php <?php
namespace BMail; namespace Bluescale\Mail;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
class BMailServiceProvider extends ServiceProvider class BluescaleMailServiceProvider extends ServiceProvider
{ {
/** /**
* Register any application services. * Register any application services.
@ -17,6 +17,10 @@ class BMailServiceProvider extends ServiceProvider
__DIR__ . '/../config/bmail.php', __DIR__ . '/../config/bmail.php',
'bmail' 'bmail'
); );
$this->app->singleton(BluescaleMailApi::class, function ($app) {
return new BluescaleMailApi($app['config']['bmail']['api_key']);
});
} }
/** /**

View File

@ -1,8 +1,8 @@
<?php <?php
namespace BMail; namespace Bluescale\Mail;
class BMailTemplate implements \JsonSerializable class BluescaleMailTemplate implements \JsonSerializable
{ {
/** /**
* The template for the message * The template for the message