From 1f99c1b9a12110b4bd15ddfc337f43b39b104e4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kai=20M=C3=B6ller?= Date: Fri, 1 Mar 2019 14:18:06 +0100 Subject: [PATCH 1/3] Task: emailFinisher sends multipart email --- Classes/Finishers/EmailFinisher.php | 96 +++++++++++++++++++++++------ 1 file changed, 77 insertions(+), 19 deletions(-) diff --git a/Classes/Finishers/EmailFinisher.php b/Classes/Finishers/EmailFinisher.php index 05bb91c..61c868d 100644 --- a/Classes/Finishers/EmailFinisher.php +++ b/Classes/Finishers/EmailFinisher.php @@ -27,6 +27,10 @@ * * - templatePathAndFilename (mandatory if "templateSource" option is not set): Template path and filename for the mail body * - templateSource (mandatory if "templatePathAndFilename" option is not set): The raw Fluid template + * - htmlTemplatePathAndFilename (mandatory if "htmlTemplateSource" option is not set): Template path and filename for the html mail body + * - htmlTemplateSource (mandatory if "htmlTemplatePathAndFilename" option is not set): The raw Fluid template for html mail body + * - plaintextTemplatePathAndFilename (mandatory if "plaintextTemplateSource" option is not set): Template path and filename for the plaintext mail body + * - plaintextTemplateSource (mandatory if "plaintextTemplatePathAndFilename" option is not set): The raw Fluid template for plaintext mail body * - layoutRootPath: root path for the layouts * - partialRootPath: root path for the partials * - variables: associative array of variables which are available inside the Fluid template @@ -54,6 +58,9 @@ class EmailFinisher extends AbstractFinisher { const FORMAT_PLAINTEXT = 'plaintext'; const FORMAT_HTML = 'html'; + const FORMAT_MULTIPART = 'multipart'; + const CONTENT_TYPE_PLAINTEXT = 'text/plain'; + const CONTENT_TYPE_HTML = 'text/html'; /** * @var Service @@ -61,6 +68,11 @@ class EmailFinisher extends AbstractFinisher */ protected $i18nService; + protected $formatContentTypes = [ + self::FORMAT_HTML => self::CONTENT_TYPE_HTML, + self::FORMAT_PLAINTEXT => self::CONTENT_TYPE_PLAINTEXT, + ]; + /** * @var array */ @@ -85,13 +97,6 @@ protected function executeInternal() if (!class_exists(SwiftMailerMessage::class)) { throw new FinisherException('The "neos/swiftmailer" doesn\'t seem to be installed, but is required for the EmailFinisher to work!', 1503392532); } - $formRuntime = $this->finisherContext->getFormRuntime(); - $standaloneView = $this->initializeStandaloneView(); - $standaloneView->assign('form', $formRuntime); - $referrer = $formRuntime->getRequest()->getHttpRequest()->getUri(); - $standaloneView->assign('referrer', $referrer); - $message = $standaloneView->render(); - $subject = $this->parseOption('subject'); $recipientAddress = $this->parseOption('recipientAddress'); $recipientName = $this->parseOption('recipientName'); @@ -102,6 +107,7 @@ protected function executeInternal() $blindCarbonCopyAddress = $this->parseOption('blindCarbonCopyAddress'); $format = $this->parseOption('format'); $testMode = $this->parseOption('testMode'); + $messages = $this->getMessages($format); if ($subject === null) { throw new FinisherException('The option "subject" must be set for the EmailFinisher.', 1327060320); @@ -140,11 +146,7 @@ protected function executeInternal() $mail->setBcc($blindCarbonCopyAddress); } - if ($format === self::FORMAT_PLAINTEXT) { - $mail->setBody($message, 'text/plain'); - } else { - $mail->setBody($message, 'text/html'); - } + $this->addMessages($mail, $messages); $this->addAttachments($mail); if ($testMode === true) { @@ -155,7 +157,7 @@ protected function executeInternal() 'replyToAddress' => $replyToAddress, 'carbonCopyAddress' => $carbonCopyAddress, 'blindCarbonCopyAddress' => $blindCarbonCopyAddress, - 'message' => $message, + 'message' => $messages, 'format' => $format, ), 'E-Mail "' . $subject . '"' @@ -165,20 +167,76 @@ protected function executeInternal() } } + protected function addMessages(SwiftMailerMessage $mail, $messages): void + { + foreach ($messages as $messageFormat => $message) { + if (count($messages) === 1) { + $mail->setBody($message, $this->formatContentTypes[$messageFormat]); + } else { + $mail->addPart($message, $this->formatContentTypes[$messageFormat]); + } + } + } + + protected function getMessages(string $format): array + { + $messages = []; + if ($format === self::FORMAT_MULTIPART) { + $messages[self::FORMAT_HTML] = $this->createMessage(self::FORMAT_HTML); + $messages[self::FORMAT_PLAINTEXT] = $this->createMessage(self::FORMAT_PLAINTEXT); + } else if ($format === self::FORMAT_PLAINTEXT) { + $messages[self::FORMAT_PLAINTEXT] = $this->createMessage(self::FORMAT_PLAINTEXT); + } else { + $messages[self::FORMAT_HTML] = $this->createMessage(self::FORMAT_HTML); + } + + return $messages; + } + + protected function createMessage(string $format): string + { + $formRuntime = $this->finisherContext->getFormRuntime(); + $standaloneView = $this->initializeStandaloneView($format); + $standaloneView->assign('form', $formRuntime); + $referrer = $formRuntime->getRequest()->getHttpRequest()->getUri(); + $standaloneView->assign('referrer', $referrer); + + return $standaloneView->render(); + } + /** + * @param string $format * @return StandaloneView * @throws FinisherException + * @throws \Neos\FluidAdaptor\Exception */ - protected function initializeStandaloneView() + protected function initializeStandaloneView(string $format = ''): StandaloneView { + $templatePathAndFilenameOption = 'templatePathAndFilename'; + $templateSourceOption = 'templateSource'; + $isSingleTemplate = isset($this->options[$templatePathAndFilenameOption]) || isset($this->options[$templateSourceOption]); + + if (!$isSingleTemplate && in_array($format, [self::FORMAT_PLAINTEXT, self::FORMAT_HTML])) { + $templatePathAndFilenameOption = $format . ucfirst($templatePathAndFilenameOption); + $templateSourceOption = $format . ucfirst($templateSourceOption); + } + $standaloneView = new StandaloneView(); - if (isset($this->options['templatePathAndFilename'])) { - $templatePathAndFilename = $this->i18nService->getLocalizedFilename($this->options['templatePathAndFilename']); + if (isset($this->options[$templatePathAndFilenameOption])) { + $templatePathAndFilename = $this->i18nService->getLocalizedFilename($this->options[$templatePathAndFilenameOption]); $standaloneView->setTemplatePathAndFilename($templatePathAndFilename[0]); - } elseif (isset($this->options['templateSource'])) { - $standaloneView->setTemplateSource($this->options['templateSource']); + } elseif (isset($this->options[$templateSourceOption])) { + $standaloneView->setTemplateSource($this->options[$templateSourceOption]); } else { - throw new FinisherException('The option "templatePathAndFilename" or "templateSource" must be set for the EmailFinisher.', 1327058829); + $options = [ + 'templatePathAndFilename', + 'templateSource', + self::FORMAT_PLAINTEXT . 'TemplatePathAndFilename', + self::FORMAT_PLAINTEXT . 'TemplateSource', + self::FORMAT_HTML . 'TemplatePathAndFilename', + self::FORMAT_HTML . 'TemplateSource' + ]; + throw new FinisherException(sprintf('One of the option "%s" must be set for the EmailFinisher.', implode('", "', $options)), 1551371435); } From 39d0af17e86a39002dd197242d5f7a21310892f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kai=20M=C3=B6ller?= Date: Fri, 1 Mar 2019 14:22:27 +0100 Subject: [PATCH 2/3] Task: fix attribute type --- Classes/Finishers/EmailFinisher.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Classes/Finishers/EmailFinisher.php b/Classes/Finishers/EmailFinisher.php index 61c868d..3f481ba 100644 --- a/Classes/Finishers/EmailFinisher.php +++ b/Classes/Finishers/EmailFinisher.php @@ -167,7 +167,7 @@ protected function executeInternal() } } - protected function addMessages(SwiftMailerMessage $mail, $messages): void + protected function addMessages(SwiftMailerMessage $mail, array $messages): void { foreach ($messages as $messageFormat => $message) { if (count($messages) === 1) { From bb50859d8ac66ef0ec06427544bf75ba20044f24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kai=20M=C3=B6ller?= Date: Fri, 1 Mar 2019 14:41:00 +0100 Subject: [PATCH 3/3] Task: Fix ci style --- Classes/Finishers/EmailFinisher.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Classes/Finishers/EmailFinisher.php b/Classes/Finishers/EmailFinisher.php index 3f481ba..0e1b924 100644 --- a/Classes/Finishers/EmailFinisher.php +++ b/Classes/Finishers/EmailFinisher.php @@ -184,7 +184,7 @@ protected function getMessages(string $format): array if ($format === self::FORMAT_MULTIPART) { $messages[self::FORMAT_HTML] = $this->createMessage(self::FORMAT_HTML); $messages[self::FORMAT_PLAINTEXT] = $this->createMessage(self::FORMAT_PLAINTEXT); - } else if ($format === self::FORMAT_PLAINTEXT) { + } elseif ($format === self::FORMAT_PLAINTEXT) { $messages[self::FORMAT_PLAINTEXT] = $this->createMessage(self::FORMAT_PLAINTEXT); } else { $messages[self::FORMAT_HTML] = $this->createMessage(self::FORMAT_HTML);