From e339ba394ecee1c8cd62d98d927291a6c080c15c Mon Sep 17 00:00:00 2001 From: jekuaitk Date: Mon, 2 Sep 2024 15:07:04 +0200 Subject: [PATCH] 1584: Avoid sending original email when size threshold is surpassed --- .../OS2FormsEmailWebformHandler.php | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/web/modules/custom/os2forms_email_handler/src/Plugin/WebformHandler/OS2FormsEmailWebformHandler.php b/web/modules/custom/os2forms_email_handler/src/Plugin/WebformHandler/OS2FormsEmailWebformHandler.php index 147d3942..321cf68e 100644 --- a/web/modules/custom/os2forms_email_handler/src/Plugin/WebformHandler/OS2FormsEmailWebformHandler.php +++ b/web/modules/custom/os2forms_email_handler/src/Plugin/WebformHandler/OS2FormsEmailWebformHandler.php @@ -51,11 +51,13 @@ public function sendMessage(WebformSubmissionInterface $webform_submission, arra $webform = $webform_submission->getWebform(); $settings = $webform->getThirdPartySetting('os2forms', WebformHelper::MODULE_NAME); + $sendOriginalMessage = TRUE; + if ($settings['enabled'] && !empty($settings['email_recipients'])) { - $this->handleAttachmentNotification($webform_submission, $message, $settings['email_recipients']); + $sendOriginalMessage = !$this->handleAttachmentNotification($webform_submission, $message, $settings['email_recipients']); } - return parent::sendMessage($webform_submission, $message); + return $sendOriginalMessage ? parent::sendMessage($webform_submission, $message) : FALSE; } /** @@ -67,11 +69,18 @@ public function sendMessage(WebformSubmissionInterface $webform_submission, arra * An array of message parameters. * @param string $emails * A string of emails. + * + * @return bool + * Whether file size threshold was surpassed or not. */ - private function handleAttachmentNotification(WebformSubmissionInterface $webform_submission, array $message, string $emails): void { - if ($this->isAttachmentFileSizeThresholdSurpassed($webform_submission)) { + private function handleAttachmentNotification(WebformSubmissionInterface $webform_submission, array $message, string $emails): bool { + $isFileSizeThresholdSurpassed = $this->isAttachmentFileSizeThresholdSurpassed($webform_submission); + + if ($isFileSizeThresholdSurpassed) { $this->sendFileSizeNotification($webform_submission, $message, $emails); } + + return $isFileSizeThresholdSurpassed; } /** @@ -210,11 +219,12 @@ private function sendFileSizeNotification(WebformSubmissionInterface $webform_su $context = [ '@form' => $this->getWebform()->label(), + '@form_id' => $this->getWebform()->id(), '@handler' => $this->label(), + '@handler_id' => $this->getHandlerId(), '@email' => $emailAddress, - 'link' => ($webform_submission->id()) ? $webform_submission->toLink($this->t('View'))->toString() : NULL, + 'link' => ($webform_submission->id()) ? $webform_submission->toLink($this->t('view'))->toString() : NULL, 'webform_submission' => $webform_submission, - 'handler_id' => $this->getHandlerId(), 'operation' => 'notification email', ]; @@ -236,11 +246,13 @@ private function sendFileSizeNotification(WebformSubmissionInterface $webform_su $notificationMessage['subject'] = $this->t('File size submission warning'); $notificationMessage['body'] = $this->t( - "

Dear @name

Submission @submission attempted sending an email with a large total file size of attachments surpassing @threshold for handler (@handler) on form (@form).

", [ + "

Dear @name

Submission @submission attempted sending an email with a large total file size of attachments surpassing @threshold for handler @handler (@handler_id) on form @form (@form_id).

", [ '@name' => $emailAddress, '@submission' => $context['link'], '@handler' => $context['@handler'], - '@form' => $context['@form'] ?? '', + '@handler_id' => $context['@handler_id'], + '@form' => $context['@form'], + '@form_id' => $context['@form_id'], '@threshold' => $settings['notification_file_size_threshold'] ?? self::DEFAULT_ATTACHMENT_FILE_SIZE_THRESHOLD, ]);