From 979a965aec5f543e353ddc3dc2ef8f96c238a834 Mon Sep 17 00:00:00 2001 From: nathan Date: Wed, 23 Aug 2023 12:59:04 -0600 Subject: [PATCH] Mail: If there are multiple attachments with the same file name, give them unique names when integrating to another app --- mail/inc/class.mail_integration.inc.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/mail/inc/class.mail_integration.inc.php b/mail/inc/class.mail_integration.inc.php index fbc1ebb625..97d3bd3fb5 100644 --- a/mail/inc/class.mail_integration.inc.php +++ b/mail/inc/class.mail_integration.inc.php @@ -354,10 +354,24 @@ class mail_integration { // Get attachments ready for integration as link if (is_array($mailcontent['attachments'])) { + $data_attachments = array(); foreach($mailcontent['attachments'] as $key => $attachment) { + // Find a unique name, in case there are multiple attachments with the same filename + $name = $attachment['filename'] ?? $mailcontent['attachments'][$key]['name']; + $i = 1; + $info = pathinfo($name); + while(count(array_filter($data_attachments, function ($v) use ($name) + { + return $v['name'] == $name; + }, ARRAY_FILTER_USE_BOTH) + ) > 0) + { + $name = $info['filename'] . " ($i)." . $info['extension']; + $i++; + } $data_attachments[$key] = array( - 'name' => $attachment['filename'] ?? $mailcontent['attachments'][$key]['name'], + 'name' => $name, 'type' => $mailcontent['attachments'][$key]['type'], 'size' => $mailcontent['attachments'][$key]['size'], 'tmp_name' => $mailcontent['attachments'][$key]['tmp_name']