From 3b82c6141ef626cd767b7faa867d875b91cadeae Mon Sep 17 00:00:00 2001 From: sanja <52755494+sanjacornelius@users.noreply.github.com> Date: Thu, 16 Jul 2026 11:44:28 -0700 Subject: [PATCH] Include file attachments in Graph payload Add convertAttachments() to MicrosoftGraphMessageConverter to map Symfony Email attachments to Microsoft Graph fileAttachment objects (name, contentType, contentBytes base64) and include them in toSendMailPayload when present. --- .../Mail/MicrosoftGraphMessageConverter.php | 21 +++++++++ .../MicrosoftGraphMessageConverterTest.php | 43 +++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 tests/unit/ProcessMaker/Mail/MicrosoftGraphMessageConverterTest.php diff --git a/ProcessMaker/Mail/MicrosoftGraphMessageConverter.php b/ProcessMaker/Mail/MicrosoftGraphMessageConverter.php index e95a3ea281..a87205cdcc 100644 --- a/ProcessMaker/Mail/MicrosoftGraphMessageConverter.php +++ b/ProcessMaker/Mail/MicrosoftGraphMessageConverter.php @@ -31,6 +31,11 @@ public static function toSendMailPayload(Email $email): array $message['bccRecipients'] = $bccRecipients; } + $attachments = self::convertAttachments($email); + if ($attachments) { + $message['attachments'] = $attachments; + } + return [ 'message' => $message, 'saveToSentItems' => true, @@ -52,4 +57,20 @@ private static function convertAddresses(array $addresses): array return ['emailAddress' => $emailAddress]; }, $addresses); } + + private static function convertAttachments(Email $email): array + { + $attachments = []; + + foreach ($email->getAttachments() as $attachment) { + $attachments[] = [ + '@odata.type' => '#microsoft.graph.fileAttachment', + 'name' => $attachment->getFilename() ?: 'attachment', + 'contentType' => $attachment->getMediaType() . '/' . $attachment->getMediaSubtype(), + 'contentBytes' => base64_encode($attachment->getBody()), + ]; + } + + return $attachments; + } } diff --git a/tests/unit/ProcessMaker/Mail/MicrosoftGraphMessageConverterTest.php b/tests/unit/ProcessMaker/Mail/MicrosoftGraphMessageConverterTest.php new file mode 100644 index 0000000000..71d172a2ca --- /dev/null +++ b/tests/unit/ProcessMaker/Mail/MicrosoftGraphMessageConverterTest.php @@ -0,0 +1,43 @@ +to('recipient@example.com') + ->subject('With attachment') + ->html('
Hello
') + ->attach('file-contents', 'report.txt', 'text/plain'); + + $payload = MicrosoftGraphMessageConverter::toSendMailPayload($email); + + $this->assertSame('With attachment', $payload['message']['subject']); + $this->assertSame('HTML', $payload['message']['body']['contentType']); + $this->assertCount(1, $payload['message']['attachments']); + $this->assertSame([ + '@odata.type' => '#microsoft.graph.fileAttachment', + 'name' => 'report.txt', + 'contentType' => 'text/plain', + 'contentBytes' => base64_encode('file-contents'), + ], $payload['message']['attachments'][0]); + } + + public function testToSendMailPayloadOmitsAttachmentsKeyWhenNonePresent() + { + $email = (new Email()) + ->to('recipient@example.com') + ->subject('No attachment') + ->text('Hello'); + + $payload = MicrosoftGraphMessageConverter::toSendMailPayload($email); + + $this->assertArrayNotHasKey('attachments', $payload['message']); + } +}