feat: mail templates package

This commit is contained in:
2022-06-08 14:41:57 +02:00
commit 655ffce923
13 changed files with 1287 additions and 0 deletions

213
src/MailTemplate.php Normal file
View File

@@ -0,0 +1,213 @@
<?php
namespace Bluesquare\MailTemplates;
class MailTemplate implements \JsonSerializable
{
/**
* The template for the message
*
* @var string
*/
public $templateId;
/**
* The sender for the message
*
* @var array
*/
public $sender = [];
/**
* The recipients for the message
*
* @var array
*/
public $recipients = [];
/**
* The attachments for the message
*
* @var array
*/
public $attachments = [];
/**
* The reply address for the message
*
* @var array|null
*/
public $replyTo = null;
/**
* The parameters for the message
*
* @var array
*/
public $parameters = [];
public static function make(string $templateId)
{
return new self($templateId);
}
public function __construct(string $templateId)
{
$this->templateId = $templateId;
$this->sender(
config('mail_templates.sender.email'),
config('mail_templates.sender.name')
);
}
/**
* @param string $address
* @param string|null $name
* @return MailTemplate
*/
public function sender(string $address, string $name = null): MailTemplate
{
$this->sender['address'] = $address;
if (! empty($name))
$this->sender['name'] = $name;
return $this;
}
/**
* @param string $address
* @param string|null $name
* @return MailTemplate
*/
public function replyTo(string $address, string $name = null): MailTemplate
{
$this->replyTo['address'] = $address;
if (! empty($name))
$this->replyTo['name'] = $name;
return $this;
}
/**
* @param array $recipients
* @return MailTemplate
*/
public function recipients(array $recipients): MailTemplate
{
if (!empty($recipients)) {
foreach ($recipients as $recipient) {
if (!empty($recipient[0]) && is_string($recipient[0])) {
$data = [
'address' => $recipient[0]
];
if (!empty($recipient[1])) {
if (!empty($recipient[1]['name']))
$data['name'] = $recipient[1]['name'];
if (!empty($recipient[1]['parameters']))
$data['parameters'] = $recipient[1]['parameters'];
}
$this->recipients[] = $data;
}
}
}
return $this;
}
/**
* @param string $address
* @param array $informations
* @return MailTemplate
*/
public function addRecipient(string $address, array $informations = []): MailTemplate
{
$data = [
'address' => $address
];
if (!empty($informations)) {
if (!empty($informations['name']))
$data['name'] = $informations['name'];
if (!empty($informations['parameters']))
$data['parameters'] = $informations['parameters'];
}
$this->recipients[] = $data;
return $this;
}
/**
* @param array $parameters
* @return MailTemplate
*/
public function parameters(array $parameters): MailTemplate
{
$this->parameters = $parameters;
return $this;
}
/**
* @param array $attachments
* @return MailTemplate
*/
public function attachments(array $attachments): MailTemplate
{
foreach ($attachments as $attachment) {
$this->addAttachment($attachment['path'] ?? $attachment['file'], $attachment['filename'] ?? null);
}
return $this;
}
public function addAttachment($file, $filename = null)
{
$file = is_object($file) && method_exists($file, 'getRealPath') ? $file->getRealPath() : $file;
$this->attachments[] = [
'path' => $file,
'filename' => $filename ?? basename($file)
];
return $this;
}
/**
* @param string $key
* @param string $value
* @return MailTemplate
*/
public function addParameter(string $key, string $value): MailTemplate
{
$this->parameters[$key] = $value;
return $this;
}
/**
* Get an array representation of the message.
*
* @return array
*/
public function toArray(): array
{
return [
'templateId' => $this->templateId,
'sender' => $this->sender,
'replyTo' => $this->replyTo,
'recipients' => $this->recipients,
'attachments' => $this->attachments,
'parameters' => $this->parameters,
];
}
public function jsonSerialize(): mixed
{
return $this->toArray();
}
}

View File

@@ -0,0 +1,28 @@
<?php
namespace Bluesquare\MailTemplates;
use Bluesquare\MailTemplates\Providers\TemplateMailProvider;
use Illuminate\Notifications\Notification;
class MailTemplatesChannel
{
protected $provider;
public function __construct(TemplateMailProvider $provider)
{
$this->provider = $provider;
}
/**
* @param $notifiable
* @param Notification $notification
* @throws MailTemplatesException
*/
public function send($notifiable, Notification $notification)
{
$template = $notification->toTemplate($notifiable);
$this->provider->send($template, $notifiable);
}
}

View File

@@ -0,0 +1,5 @@
<?php
namespace Bluesquare\MailTemplates;
class MailTemplatesException extends \Exception {}

View File

@@ -0,0 +1,39 @@
<?php
namespace Bluesquare\MailTemplates;
use Bluesquare\MailTemplates\Providers\BluesquareMailProvider;
use Bluesquare\MailTemplates\Providers\LogsProvider;
use Bluesquare\MailTemplates\Providers\SendinblueProvider;
use Bluesquare\MailTemplates\Providers\TemplateMailProvider;
use Illuminate\Support\ServiceProvider;
class MailTemplatesServiceProvider extends ServiceProvider
{
public function register()
{
$this->mergeConfigFrom(
__DIR__ . '/../config/mail_templates.php',
'mail_templates'
);
$this->app->singleton(TemplateMailProvider::class, function ($app) {
$config = config('mail_templates');
switch ($config['provider']) {
case 'logs': return new LogsProvider($config);
case 'sendinblue': return new SendinblueProvider($config);
case 'bluesquare': return new BluesquareMailProvider($config);
}
throw new MailTemplatesException("Unknown provider {$config['provider']}");
});
}
public function boot()
{
$this->publishes([
__DIR__ . '/../config/mail_templates.php' => config_path('mail_templates.php')
], 'config');
}
}

View File

@@ -0,0 +1,8 @@
<?php
namespace Bluesquare\MailTemplates\Providers;
class BluesquareMailProvider extends LogsProvider
{
//@TODO
}

View File

@@ -0,0 +1,24 @@
<?php
namespace Bluesquare\MailTemplates\Providers;
use Bluesquare\MailTemplates\MailTemplate;
use Illuminate\Support\Facades\Log;
class LogsProvider implements TemplateMailProvider
{
protected $config;
public function __construct(array $config)
{
$this->config = $config;
}
public function send(MailTemplate $template, $notifiable = null)
{
Log::debug("Mail Templates: send()", [
'template' => $template->toArray(),
'notifiable' => $notifiable
]);
}
}

View File

@@ -0,0 +1,74 @@
<?php
namespace Bluesquare\MailTemplates\Providers;
use Bluesquare\MailTemplates\MailTemplate;
use SendinBlue\Client\Api\TransactionalEmailsApi;
use SendinBlue\Client\Model\SendSmtpEmail;
use SendinBlue\Client\Model\SendSmtpEmailAttachment;
use SendinBlue\Client\Model\SendSmtpEmailReplyTo;
use SendinBlue\Client\Model\SendSmtpEmailSender;
use SendinBlue\Client\Model\SendSmtpEmailTo;
class SendinblueProvider implements TemplateMailProvider
{
protected $api;
protected $config;
public function __construct(array $config)
{
$this->config = $config;
$api_config = \SendinBlue\Client\Configuration::getDefaultConfiguration()
->setApiKey('api-key', $config['api_key']);
if (! empty($config['api_url']))
$api_config->setHost($config['api_url']);
$this->api = new TransactionalEmailsApi(
new \GuzzleHttp\Client(),
$api_config
);
}
public function send(MailTemplate $template, $notifiable = null)
{
$data = $template->toArray();
$model = new SendSmtpEmail();
$template = $data['templateId'];
if (isset($config['map']['sendinblue']) && $config['map']['sendinblue'][$template]) {
$template = $config['map']['sendinblue'][$template];
}
$model->setTemplateId($template);
$model->setParams($data['parameters']);
$model->setSender(new SendSmtpEmailSender($data['sender']));
if ($data['replyTo'])
$model->setReplyTo(new SendSmtpEmailReplyTo($data['replyTo']));
$model->setTo(array_map(function ($to) {
return new SendSmtpEmailTo($to);
}, $data['recipients']));
if (! empty($config['redirect']))
$model->setTo([
new SendSmtpEmailTo([
'email' => $config['redirect']
])
]);
$model->setAttachment(array_map(function ($item) {
$attachment = new SendSmtpEmailAttachment();
$attachment->setName($item['filename']);
$attachment->setContent(file_get_contents($item['path']));
return $attachment;
}, $data['attachments']));
$this->api->sendTransacEmail($model);
}
}

View File

@@ -0,0 +1,12 @@
<?php
namespace Bluesquare\MailTemplates\Providers;
use Bluesquare\MailTemplates\MailTemplate;
interface TemplateMailProvider
{
public function __construct(array $config);
public function send(MailTemplate $template, $notifiable = null);
}