You've already forked laravel-mail
Renaming
This commit is contained in:
64
src/BluescaleMailApi.php
Normal file
64
src/BluescaleMailApi.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?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 BMailException
|
||||
*/
|
||||
public function send($template, $notifiable): StreamInterface
|
||||
{
|
||||
$url = config('bmail.api_url') ?? 'https://bluescale.email/api/project/template/send';
|
||||
|
||||
$client = new Client();
|
||||
|
||||
$notifiable_recipient = false;
|
||||
if (is_object($notifiable) && $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());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user