From 26a48162ff0e5c9fccb3d764bd449ec2e8309429 Mon Sep 17 00:00:00 2001 From: Michal Kleiner Date: Mon, 13 Nov 2023 00:21:33 +1300 Subject: [PATCH] Add extension hook to control file attachments per recipient and field --- code/Control/UserDefinedFormController.php | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/code/Control/UserDefinedFormController.php b/code/Control/UserDefinedFormController.php index deffc32..201cc18 100644 --- a/code/Control/UserDefinedFormController.php +++ b/code/Control/UserDefinedFormController.php @@ -355,7 +355,8 @@ JS // attach a file to recipient email only if lower than configured size if ($file->getAbsoluteSize() <= $this->getMaximumAllowedEmailAttachmentSize()) { - $attachments[] = $file; + // using the field name as array index is fine as file upload field only allows one file + $attachments[$field->Name] = $file; } } } @@ -393,17 +394,22 @@ JS $mergeFields = $this->getMergeFieldsMap($emailData['Fields']); if ($attachments && (bool) $recipient->HideFormData === false) { - foreach ($attachments as $file) { + foreach ($attachments as $uploadFieldName => $file) { /** @var File $file */ if ((int) $file->ID === 0) { continue; } - $email->addAttachmentFromData( - $file->getString(), - $file->getFilename(), - $file->getMimeType() - ); + $canAttachFileForRecipient = true; + $this->extend('updateCanAttachFileForRecipient', $canAttachFileForRecipient, $recipient, $uploadFieldName, $file); + + if ($canAttachFileForRecipient) { + $email->addAttachmentFromData( + $file->getString(), + $file->getFilename(), + $file->getMimeType() + ); + } } }