You've already forked laravel-mail-templates
feat: mail templates package
This commit is contained in:
74
src/Providers/SendinblueProvider.php
Normal file
74
src/Providers/SendinblueProvider.php
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user