diff --git a/src/BluescaleMailApi.php b/src/BluescaleMailApi.php index fa734a0..a0f9d16 100644 --- a/src/BluescaleMailApi.php +++ b/src/BluescaleMailApi.php @@ -54,27 +54,37 @@ class BluescaleMailApi $multipart_form = []; - foreach ($template as $key => $value) { // Formatting whole template to pass in 'multipart' form + foreach ($template as $key => $value) + { + if ($key == 'attachments') + { + foreach ($value as $i => $attachment) { + $multipart_form[] = [ + 'name' => "{$key}[$i]", + 'contents' => file_get_contents($attachment['path']), + 'filename' => $attachment['filename'] + ]; + } + + continue; + } + 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]; - - 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; + foreach ($value as $subkey => $subvalue) + { + $multipart_form[] = [ + 'name' => "{$key}[$subkey]" . (is_array($subvalue) ? '[' . key($subvalue) . ']' : '' ), + 'contents' => (is_array($subvalue) ? reset($subvalue) : $subvalue) + ]; } } + dump($multipart_form); + try { return $client->request('post', $url, [ 'multipart' => $multipart_form, diff --git a/src/BluescaleMailTemplate.php b/src/BluescaleMailTemplate.php index 4e76b6d..6ecb885 100644 --- a/src/BluescaleMailTemplate.php +++ b/src/BluescaleMailTemplate.php @@ -46,6 +46,11 @@ class BluescaleMailTemplate implements \JsonSerializable */ public $parameters = []; + public static function make(string $template_id) + { + return new self($template_id); + } + public function __construct(string $template_id) { $this->template_id = $template_id; @@ -164,6 +169,8 @@ class BluescaleMailTemplate implements \JsonSerializable 'path' => $file, 'filename' => $filename ?? basename($file) ]; + + return $this; } /**