From e0a805cbdb9c5f4f05169667c460d9fa0f1382e3 Mon Sep 17 00:00:00 2001 From: Thifaine Noirault Date: Fri, 29 May 2020 12:00:33 +0200 Subject: [PATCH] feat: handle attachments --- src/BluescaleMailApi.php | 27 +++++++++++++++++++++++---- src/BluescaleMailTemplate.php | 8 ++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/BluescaleMailApi.php b/src/BluescaleMailApi.php index 06630c4..5d70882 100644 --- a/src/BluescaleMailApi.php +++ b/src/BluescaleMailApi.php @@ -37,24 +37,43 @@ class BluescaleMailApi $notifiable_recipient = false; $notifiable_email = $notifiable->email; $recipients = $template->recipients; + $attachments = $template->attachments; 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 ]; } + $files = []; + if (count($attachments) > 0) + foreach ($attachments as $attachment) + $files[] = [ + 'name' => $attachment['name'], + 'contents' => $attachment['content'], + 'filename' => $attachment['filename'], + ]; try { - return $client->request('post', $url, [ - 'form_params' => $template, - 'headers' => [ + if (count($attachments) > 0) { + return $client->request('post', $url, [ + 'multipart' => $files, + 'form_params' => $template, + 'headers' => [ + 'Authorization' => 'Bearer ' . $this->api_key, + 'Accept' => 'application/json' + ] + ])->getBody(); + } + + return $client->request('post', $url, [ + 'form_params' => $template, + 'headers' => [ 'Authorization' => 'Bearer ' . $this->api_key, 'Accept' => 'application/json' ] diff --git a/src/BluescaleMailTemplate.php b/src/BluescaleMailTemplate.php index ef09161..aa63a8f 100644 --- a/src/BluescaleMailTemplate.php +++ b/src/BluescaleMailTemplate.php @@ -26,6 +26,13 @@ class BluescaleMailTemplate implements \JsonSerializable public $recipients = []; /** + * The attachments for the message + * + * @var array + */ + public $attachments = []; + + /** * The reply address for the message * * @var array @@ -159,6 +166,7 @@ class BluescaleMailTemplate implements \JsonSerializable 'sender' => $this->sender, 'replyTo' => $this->replyTo, 'recipients' => $this->recipients, + 'attachments' => $this->attachments, 'parameters' => $this->parameters, ]; }