From e0a805cbdb9c5f4f05169667c460d9fa0f1382e3 Mon Sep 17 00:00:00 2001 From: Thifaine Noirault Date: Fri, 29 May 2020 12:00:33 +0200 Subject: [PATCH 01/12] 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, ]; } From c4ccfd7310b3816ddf9ac0b58da6bcfd5248a7ee Mon Sep 17 00:00:00 2001 From: Thifaine Noirault Date: Fri, 29 May 2020 15:53:43 +0200 Subject: [PATCH 02/12] feat: add attachments method --- src/BluescaleMailTemplate.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/BluescaleMailTemplate.php b/src/BluescaleMailTemplate.php index aa63a8f..5f3f3fe 100644 --- a/src/BluescaleMailTemplate.php +++ b/src/BluescaleMailTemplate.php @@ -144,6 +144,16 @@ class BluescaleMailTemplate implements \JsonSerializable } /** + * @param array $attachments + * @return BluescaleMailTemplate + */ + public function attachments(array $attachments): BluescaleMailTemplate + { + $this->attachments = $attachments; + return $this; + } + + /** * @param string $key * @param string $value * @return BluescaleMailTemplate From 6c1aedd2ae54a6714ca63660b740dfbd07edf3f7 Mon Sep 17 00:00:00 2001 From: Thifaine Noirault Date: Fri, 29 May 2020 17:32:21 +0200 Subject: [PATCH 03/12] feat: forward attachments instead --- src/BluescaleMailApi.php | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/src/BluescaleMailApi.php b/src/BluescaleMailApi.php index 5d70882..3a3cfbd 100644 --- a/src/BluescaleMailApi.php +++ b/src/BluescaleMailApi.php @@ -37,7 +37,6 @@ class BluescaleMailApi $notifiable_recipient = false; $notifiable_email = $notifiable->email; $recipients = $template->recipients; - $attachments = $template->attachments; if (count($recipients) > 0) foreach ($recipients as $recipient) @@ -50,27 +49,8 @@ class BluescaleMailApi 'address' => $notifiable->email ]; } - $files = []; - if (count($attachments) > 0) - foreach ($attachments as $attachment) - $files[] = [ - 'name' => $attachment['name'], - 'contents' => $attachment['content'], - 'filename' => $attachment['filename'], - ]; try { - 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' => [ From 033ee647247d528a240ac7209f50366cae6307e8 Mon Sep 17 00:00:00 2001 From: Loann Meignant Date: Mon, 8 Jun 2020 12:00:43 +0200 Subject: [PATCH 04/12] Modifying API --- src/BluescaleMailApi.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/BluescaleMailApi.php b/src/BluescaleMailApi.php index 3a3cfbd..e816cca 100644 --- a/src/BluescaleMailApi.php +++ b/src/BluescaleMailApi.php @@ -5,6 +5,8 @@ namespace Bluescale\Mail; use Bluescale\Mail\BluescaleMailTemplate; use GuzzleHttp\Client; use Psr\Http\Message\StreamInterface; +use Symfony\Component\HttpFoundation\File\UploadedFile; + class BluescaleMailApi { @@ -37,6 +39,7 @@ class BluescaleMailApi $notifiable_recipient = false; $notifiable_email = $notifiable->email; $recipients = $template->recipients; + $attachments = $template->attachments; if (count($recipients) > 0) foreach ($recipients as $recipient) @@ -48,6 +51,16 @@ 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; + } + + $template->attachments($formatted_attachmments); + } } try { From 98790bb09a4159ddb228a43f5cad31b394574e29 Mon Sep 17 00:00:00 2001 From: Loann Meignant Date: Mon, 8 Jun 2020 12:08:19 +0200 Subject: [PATCH 05/12] Modifications --- src/BluescaleMailApi.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BluescaleMailApi.php b/src/BluescaleMailApi.php index e816cca..39bbd98 100644 --- a/src/BluescaleMailApi.php +++ b/src/BluescaleMailApi.php @@ -55,7 +55,7 @@ class BluescaleMailApi if (count($attachments) > 0) { $formatted_attachmments = []; foreach ($attachments as $attachment) { - $file = new UploadedFile($attachment->path, $attachment->fileName, 'application/octet-stream', null, null, false); + $file = new UploadedFile($attachment['path'], $attachment['filename'], 'application/octet-stream', null, null, false); $formatted_attachmments[] = $file; } From a9967a29cd5093a4e14ffabf61337e3307aee47e Mon Sep 17 00:00:00 2001 From: Loann Meignant Date: Mon, 8 Jun 2020 12:42:03 +0200 Subject: [PATCH 06/12] modifications --- src/BluescaleMailApi.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BluescaleMailApi.php b/src/BluescaleMailApi.php index 39bbd98..9be90ed 100644 --- a/src/BluescaleMailApi.php +++ b/src/BluescaleMailApi.php @@ -59,7 +59,7 @@ class BluescaleMailApi $formatted_attachmments[] = $file; } - $template->attachments($formatted_attachmments); + $template->attachments = $formatted_attachmments; } } From 5646228546e00b40ddffcee4e483484b3936e5f2 Mon Sep 17 00:00:00 2001 From: Loann Meignant Date: Mon, 8 Jun 2020 12:48:24 +0200 Subject: [PATCH 07/12] test --- src/BluescaleMailApi.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/BluescaleMailApi.php b/src/BluescaleMailApi.php index 9be90ed..f6a5679 100644 --- a/src/BluescaleMailApi.php +++ b/src/BluescaleMailApi.php @@ -55,8 +55,8 @@ class BluescaleMailApi 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; +// $file = new UploadedFile($attachment['path'], $attachment['filename'], 'application/octet-stream', null, null, false); + $formatted_attachmments[] = $attachment; } $template->attachments = $formatted_attachmments; From 4bad4261492720c74dbc77238d6168ccd0196a2d Mon Sep 17 00:00:00 2001 From: Loann Meignant Date: Mon, 8 Jun 2020 12:57:00 +0200 Subject: [PATCH 08/12] test --- src/BluescaleMailApi.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/BluescaleMailApi.php b/src/BluescaleMailApi.php index f6a5679..9be90ed 100644 --- a/src/BluescaleMailApi.php +++ b/src/BluescaleMailApi.php @@ -55,8 +55,8 @@ class BluescaleMailApi 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[] = $attachment; + $file = new UploadedFile($attachment['path'], $attachment['filename'], 'application/octet-stream', null, null, false); + $formatted_attachmments[] = $file; } $template->attachments = $formatted_attachmments; From 4a3ecb3a6170e7e33516bb3d520f0ca7a04e219c Mon Sep 17 00:00:00 2001 From: Loann Meignant Date: Mon, 8 Jun 2020 16:26:35 +0200 Subject: [PATCH 09/12] 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' From 169e5324348a96324714cb7426f09629e861e603 Mon Sep 17 00:00:00 2001 From: Loann Meignant Date: Tue, 9 Jun 2020 10:50:42 +0200 Subject: [PATCH 10/12] Modifications --- src/BluescaleMailApi.php | 28 +++++++++++----------------- src/BluescaleMailTemplate.php | 10 ++++++++++ 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/BluescaleMailApi.php b/src/BluescaleMailApi.php index 2fe9ea0..fa734a0 100644 --- a/src/BluescaleMailApi.php +++ b/src/BluescaleMailApi.php @@ -52,30 +52,24 @@ class BluescaleMailApi ]; } - $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; - } - $multipart_form = []; - foreach($template as $key => $value){ // Formatting whole template to pass in 'multipart' form - if(!is_array($value)){ + 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)]; + foreach ($value as $multiKey => $multiValue) { + $multiName = $key . '[' .$multiKey . ']' . (is_array($multiValue) ? '[' . key($multiValue) . ']' : '' ); + $data = ['name' => $multiName]; - if (substr($key, 0, 11) === "attachments") - $data['filename'] = $multiValue->getClientOriginalName(); + if ($key === "attachments") { + $data['contents'] = file_get_contents($multiValue['path']); + $data['filename'] = $multiValue['filename']; + } else { + $data['contents'] = (is_array($multiValue) ? reset($multiValue) : $multiValue); + } $multipart_form[] = $data; } diff --git a/src/BluescaleMailTemplate.php b/src/BluescaleMailTemplate.php index 5f3f3fe..07b3258 100644 --- a/src/BluescaleMailTemplate.php +++ b/src/BluescaleMailTemplate.php @@ -153,6 +153,16 @@ class BluescaleMailTemplate implements \JsonSerializable return $this; } + public function addAttachment($file, $filename = null) + { + $file = is_object($file) && method_exists($file, 'getRealPath') ? $file->getRealPath() : $file; + + $this->attachments[] = [ + 'file' => $file, + 'filename' => $filename ?? basename($file) + ]; + } + /** * @param string $key * @param string $value From 871d55f8d7f6e324db9b38db31727d2836db1d5f Mon Sep 17 00:00:00 2001 From: Loann Meignant Date: Tue, 9 Jun 2020 12:30:46 +0200 Subject: [PATCH 11/12] Modifying addAttachment() --- src/BluescaleMailTemplate.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BluescaleMailTemplate.php b/src/BluescaleMailTemplate.php index 07b3258..506b93f 100644 --- a/src/BluescaleMailTemplate.php +++ b/src/BluescaleMailTemplate.php @@ -158,7 +158,7 @@ class BluescaleMailTemplate implements \JsonSerializable $file = is_object($file) && method_exists($file, 'getRealPath') ? $file->getRealPath() : $file; $this->attachments[] = [ - 'file' => $file, + 'path' => $file, 'filename' => $filename ?? basename($file) ]; } From 7a98f99840d3928c39b51abb99046cd4a65dc339 Mon Sep 17 00:00:00 2001 From: Loann Meignant Date: Tue, 9 Jun 2020 13:09:13 +0200 Subject: [PATCH 12/12] Modifications --- src/BluescaleMailTemplate.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/BluescaleMailTemplate.php b/src/BluescaleMailTemplate.php index 506b93f..4e76b6d 100644 --- a/src/BluescaleMailTemplate.php +++ b/src/BluescaleMailTemplate.php @@ -149,7 +149,10 @@ class BluescaleMailTemplate implements \JsonSerializable */ public function attachments(array $attachments): BluescaleMailTemplate { - $this->attachments = $attachments; + foreach ($attachments as $attachment) { + $this->addAttachment($attachment['path'] ?? $attachment['file'], $attachment['filename'] ?? null); + } + return $this; }