From 4a3ecb3a6170e7e33516bb3d520f0ca7a04e219c Mon Sep 17 00:00:00 2001 From: Loann Meignant Date: Mon, 8 Jun 2020 16:26:35 +0200 Subject: [PATCH] Modifications --- src/BluescaleMailApi.php | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/src/BluescaleMailApi.php b/src/BluescaleMailApi.php index 9be90ed..2fe9ea0 100644 --- a/src/BluescaleMailApi.php +++ b/src/BluescaleMailApi.php @@ -39,7 +39,6 @@ class BluescaleMailApi $notifiable_recipient = false; $notifiable_email = $notifiable->email; $recipients = $template->recipients; - $attachments = $template->attachments; if (count($recipients) > 0) foreach ($recipients as $recipient) @@ -51,21 +50,40 @@ class BluescaleMailApi $template->recipients[] = [ 'address' => $notifiable->email ]; + } - if (count($attachments) > 0) { - $formatted_attachmments = []; - foreach ($attachments as $attachment) { - $file = new UploadedFile($attachment['path'], $attachment['filename'], 'application/octet-stream', null, null, false); - $formatted_attachmments[] = $file; - } + $attachments = $template->attachments; + if (count($attachments) > 0) { // Formatting attachments to UploadFiles + $formatted_attachmments = []; + foreach ($attachments as $attachment) { + $formatted_attachmments[] = new UploadedFile($attachment['path'], $attachment['filename'], null, null, false); + } - $template->attachments = $formatted_attachmments; + $template->attachments = $formatted_attachmments; + } + + $multipart_form = []; + + foreach($template as $key => $value){ // Formatting whole template to pass in 'multipart' form + if(!is_array($value)){ + $multipart_form[] = ['name' => $key, 'contents' => $value]; + continue; + } + + foreach($value as $multiKey => $multiValue){ + $multiName = $key . '[' .$multiKey . ']' . (is_array($multiValue) ? '[' . key($multiValue) . ']' : '' ) . ''; + $data = ['name' => $multiName, 'contents' => (is_array($multiValue) ? reset($multiValue) : $multiValue)]; + + if (substr($key, 0, 11) === "attachments") + $data['filename'] = $multiValue->getClientOriginalName(); + + $multipart_form[] = $data; } } try { return $client->request('post', $url, [ - 'form_params' => $template, + 'multipart' => $multipart_form, 'headers' => [ 'Authorization' => 'Bearer ' . $this->api_key, 'Accept' => 'application/json'