From b2fcefb7ec92cdd5a5e5c94ef8bc2dc8815d8886 Mon Sep 17 00:00:00 2001 From: Loann Meignant Date: Wed, 29 Apr 2020 18:02:40 +0200 Subject: [PATCH] Notifiable --- src/BMailApi.php | 19 ++++++++++++++++++- src/Broadcasting/BMailChannel.php | 2 +- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/BMailApi.php b/src/BMailApi.php index 1ef534b..dc6ef71 100644 --- a/src/BMailApi.php +++ b/src/BMailApi.php @@ -21,15 +21,32 @@ class BMailApi /** * @param $template + * @param $notifiable * @return \Psr\Http\Message\StreamInterface * @throws BMailException */ - public function send($template): StreamInterface + public function send($template, $notifiable): StreamInterface { $url = config('bmail.api_url'); $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, diff --git a/src/Broadcasting/BMailChannel.php b/src/Broadcasting/BMailChannel.php index b92ee3c..aca3ab7 100644 --- a/src/Broadcasting/BMailChannel.php +++ b/src/Broadcasting/BMailChannel.php @@ -17,6 +17,6 @@ class BMailChannel { $template = $notification->toTemplate($notifiable); - return (new BMailApi(config("bmail.api_key")))->send($template); + return (new BMailApi(config("bmail.api_key")))->send($template, $notifiable); } }