81 lines
2.4 KiB
PHP
81 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace Bluesquare\MailTemplates\Providers;
|
|
|
|
use Bluesquare\MailTemplates\MailTemplate;
|
|
use Illuminate\Support\Facades\Log;
|
|
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)
|
|
{
|
|
$config = $this->config;
|
|
$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'])) {
|
|
Log::info("Redirecting SIB mail to: " . $config['redirect']);
|
|
$model->setTo([
|
|
new SendSmtpEmailTo([
|
|
'email' => $config['redirect']
|
|
])
|
|
]);
|
|
}
|
|
|
|
if (! empty($data['attachments'])) {
|
|
$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);
|
|
}
|
|
}
|