68 lines
1.8 KiB
PHP
68 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace Bluescale\Mail;
|
|
|
|
use Bluescale\Mail\BluescaleMailTemplate;
|
|
use GuzzleHttp\Client;
|
|
use Psr\Http\Message\StreamInterface;
|
|
|
|
class BluescaleMailApi
|
|
{
|
|
/**
|
|
* The API key
|
|
*
|
|
* @var string
|
|
*/
|
|
private $api_key;
|
|
|
|
public function __construct(string $api_key)
|
|
{
|
|
$this->api_key = $api_key;
|
|
}
|
|
|
|
/**
|
|
* @param BluescaleMailTemplate $template
|
|
* @param $notifiable
|
|
* @return \Psr\Http\Message\StreamInterface
|
|
* @throws BluescaleMailException
|
|
*/
|
|
public function send($template, $notifiable): StreamInterface
|
|
{
|
|
$url = config('bmail.api_url') ?? 'https://bluescale.email/api/project/template/send';
|
|
|
|
$client = new Client();
|
|
|
|
if (is_object($notifiable) && isset($notifiable->email))
|
|
{
|
|
$notifiable_recipient = false;
|
|
$notifiable_email = $notifiable->email;
|
|
$recipients = $template->recipients;
|
|
|
|
if (count($recipients) > 0)
|
|
foreach ($recipients as $recipient)
|
|
if (!empty($recipient['address']))
|
|
if ($notifiable_email == $recipient['address'])
|
|
$notifiable_recipient = true;
|
|
|
|
|
|
if (!$notifiable_recipient)
|
|
$template->recipients[] = [
|
|
'address' => $notifiable->email
|
|
];
|
|
}
|
|
|
|
try {
|
|
return $client->request('post', $url, [
|
|
'form_params' => $template,
|
|
'headers' => [
|
|
'Authorization' => 'Bearer ' . $this->api_key,
|
|
'Accept' => 'application/json'
|
|
]
|
|
])->getBody();
|
|
|
|
} catch(\Exception $e) {
|
|
throw new BluescaleMailException($e->getMessage());
|
|
}
|
|
}
|
|
}
|