Modifications

This commit is contained in:
Loann M 2020-06-09 10:50:42 +02:00
parent 4a3ecb3a61
commit 169e532434
2 changed files with 21 additions and 17 deletions

View File

@ -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;
}

View File

@ -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