From c433263c11ca0563581141166fbc7c5c09e98fc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kai=20M=C3=B6ller?= Date: Wed, 28 Sep 2022 17:36:11 +0200 Subject: [PATCH 1/4] add support for multiple file upload elemnt --- Classes/Domain/Form.php | 3 ++ Classes/Runtime/Action/EmailAction.php | 47 ++++++++++++++------------ 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/Classes/Domain/Form.php b/Classes/Domain/Form.php index 79baeda..0d3d006 100644 --- a/Classes/Domain/Form.php +++ b/Classes/Domain/Form.php @@ -296,6 +296,9 @@ public function calculateHiddenFields(string $content = null): array } } elseif (substr($name, -18) == '[__collectionName]' || substr($name, -41) === '[originallySubmittedResource][__identity]') { // ignore special fields for file uploads + } elseif (substr($name, -2) == '[]') { + // handle multiple file upload field + $formFieldNames[] = substr($name, 0, strlen($name) - 2); } else { $formFieldNames[] = $name; } diff --git a/Classes/Runtime/Action/EmailAction.php b/Classes/Runtime/Action/EmailAction.php index 918a192..f8f9b55 100644 --- a/Classes/Runtime/Action/EmailAction.php +++ b/Classes/Runtime/Action/EmailAction.php @@ -93,7 +93,12 @@ public function perform(): ?ActionResponse $mail->setBody($html, 'text/html'); } - $this->addAttachments($mail); + $attachments = $this->options['attachments'] ?? null; + if (is_array($attachments)) { + foreach ($attachments as $attachment) { + $this->addAttachment($attachment, $mail); + } + } if ($testMode === true) { $response = new ActionResponse(); @@ -124,32 +129,30 @@ public function perform(): ?ActionResponse } /** + * @param mixed $attachment * @param SwiftMailerMessage $mail */ - protected function addAttachments(SwiftMailerMessage $mail): void + protected function addAttachment($attachment, SwiftMailerMessage $mail): void { - $attachments = $this->options['attachments'] ?? null; - if (is_array($attachments)) { - foreach ($attachments as $attachment) { - if (is_string($attachment)) { - $mail->attach(\Swift_Attachment::fromPath($attachment)); - } elseif (is_object($attachment) && ($attachment instanceof UploadedFileInterface)) { - $mail->attach(new \Swift_Attachment($attachment->getStream()->getContents(), $attachment->getClientFilename(), $attachment->getClientMediaType())); - } elseif (is_object($attachment) && ($attachment instanceof PersistentResource)) { - $stream = $attachment->getStream(); - if (!is_bool($stream)) { - $content = stream_get_contents($stream); - if (!is_bool($content)) { - $mail->attach(new \Swift_Attachment($content, $attachment->getFilename(), $attachment->getMediaType())); - } - } - } elseif (is_array($attachment) && isset($attachment['content']) && isset($attachment['name'])) { - $content = $attachment['content']; - $name = $attachment['name']; - $type = $attachment['type'] ?? MediaTypes::getMediaTypeFromFilename($name); - $mail->attach(new \Swift_Attachment($content, $name, $type)); + if (is_string($attachment)) { + $mail->attach(\Swift_Attachment::fromPath($attachment)); + } elseif (is_object($attachment) && ($attachment instanceof UploadedFileInterface)) { + $mail->attach(new \Swift_Attachment($attachment->getStream()->getContents(), $attachment->getClientFilename(), $attachment->getClientMediaType())); + } elseif (is_object($attachment) && ($attachment instanceof PersistentResource)) { + $stream = $attachment->getStream(); + if (!is_bool($stream)) { + $content = stream_get_contents($stream); + if (!is_bool($content)) { + $mail->attach(new \Swift_Attachment($content, $attachment->getFilename(), $attachment->getMediaType())); } } + } elseif (is_array($attachment) && isset($attachment['content']) && isset($attachment['name'])) { + $content = $attachment['content']; + $name = $attachment['name']; + $type = $attachment['type'] ?? MediaTypes::getMediaTypeFromFilename($name); + $mail->attach(new \Swift_Attachment($content, $name, $type)); + } elseif (is_array($attachment)) { + $this->addAttachment($attachment, $mail); } } } From 85f2cea16789520b9e94397f7f59d24919d3d838 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kai=20M=C3=B6ller?= Date: Thu, 29 Sep 2022 11:35:32 +0200 Subject: [PATCH 2/4] Fix email action --- .../Exception/InvalidArgumentException.php | 19 +++++++++ Classes/Runtime/Action/EmailAction.php | 42 +++++++++++++++---- 2 files changed, 52 insertions(+), 9 deletions(-) create mode 100644 Classes/Exception/InvalidArgumentException.php diff --git a/Classes/Exception/InvalidArgumentException.php b/Classes/Exception/InvalidArgumentException.php new file mode 100644 index 0000000..a78648b --- /dev/null +++ b/Classes/Exception/InvalidArgumentException.php @@ -0,0 +1,19 @@ +setBody($html, 'text/html'); } - $attachments = $this->options['attachments'] ?? null; - if (is_array($attachments)) { - foreach ($attachments as $attachment) { - $this->addAttachment($attachment, $mail); - } - } + $this->addAttachments($mail); if ($testMode === true) { $response = new ActionResponse(); @@ -128,9 +124,37 @@ public function perform(): ?ActionResponse return null; } + /** + * @param SwiftMailerMessage $mail + * @return void + */ + protected function addAttachments(SwiftMailerMessage $mail) + { + $attachments = $this->options['attachments'] ?? null; + if (is_array($attachments)) { + try { + foreach ($attachments as $attachment) { + try { + $this->addAttachment($attachment, $mail); + } catch (InvalidArgumentException $e) { + // handle files of multiple file upload element + if (is_array($attachment)) { + foreach ($attachment as $uploadItem) { + $this->addAttachment($uploadItem, $mail); + } + } + } + } + } catch (InvalidArgumentException $e) { + + } + } + } + /** * @param mixed $attachment * @param SwiftMailerMessage $mail + * @throws InvalidArgumentException */ protected function addAttachment($attachment, SwiftMailerMessage $mail): void { @@ -151,8 +175,8 @@ protected function addAttachment($attachment, SwiftMailerMessage $mail): void $name = $attachment['name']; $type = $attachment['type'] ?? MediaTypes::getMediaTypeFromFilename($name); $mail->attach(new \Swift_Attachment($content, $name, $type)); - } elseif (is_array($attachment)) { - $this->addAttachment($attachment, $mail); + } else { + throw new InvalidArgumentException('Can not handle $attachment.', 1664442569); } } -} +} \ No newline at end of file From 00a1f69e5c9a70fc45b6bf05a591afb9ced1716a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kai=20M=C3=B6ller?= Date: Thu, 29 Sep 2022 11:38:13 +0200 Subject: [PATCH 3/4] fix code style --- Classes/Runtime/Action/EmailAction.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Classes/Runtime/Action/EmailAction.php b/Classes/Runtime/Action/EmailAction.php index 9cbd34c..dee4925 100644 --- a/Classes/Runtime/Action/EmailAction.php +++ b/Classes/Runtime/Action/EmailAction.php @@ -128,7 +128,7 @@ public function perform(): ?ActionResponse * @param SwiftMailerMessage $mail * @return void */ - protected function addAttachments(SwiftMailerMessage $mail) + protected function addAttachments(SwiftMailerMessage $mail): void { $attachments = $this->options['attachments'] ?? null; if (is_array($attachments)) { From d3a5eca3384cc017319482d0726d189cf1be835d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kai=20M=C3=B6ller?= Date: Thu, 29 Sep 2022 11:39:28 +0200 Subject: [PATCH 4/4] remove return annotation --- Classes/Runtime/Action/EmailAction.php | 1 - 1 file changed, 1 deletion(-) diff --git a/Classes/Runtime/Action/EmailAction.php b/Classes/Runtime/Action/EmailAction.php index dee4925..80cd652 100644 --- a/Classes/Runtime/Action/EmailAction.php +++ b/Classes/Runtime/Action/EmailAction.php @@ -126,7 +126,6 @@ public function perform(): ?ActionResponse /** * @param SwiftMailerMessage $mail - * @return void */ protected function addAttachments(SwiftMailerMessage $mail): void {