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']); + } +}