From 3e19a0180a8564988f4b646ebb55921747829cc3 Mon Sep 17 00:00:00 2001 From: Rias Date: Thu, 11 Jul 2024 12:02:59 +0200 Subject: [PATCH 1/4] Send attachment data to Ray --- src/Payloads/LoggedMailPayload.php | 24 +++++++++++++++++++++++- tests/TestClasses/TestMailable.php | 14 ++++++++++++++ tests/Unit/MailableTest.php | 1 + 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/Payloads/LoggedMailPayload.php b/src/Payloads/LoggedMailPayload.php index 8ad50e4..18076c2 100644 --- a/src/Payloads/LoggedMailPayload.php +++ b/src/Payloads/LoggedMailPayload.php @@ -8,6 +8,7 @@ use ZBateson\MailMimeParser\Header\Part\AddressPart; use ZBateson\MailMimeParser\IMessage; use ZBateson\MailMimeParser\MailMimeParser; +use ZBateson\MailMimeParser\Message\MimePart; class LoggedMailPayload extends Payload { @@ -29,6 +30,9 @@ class LoggedMailPayload extends Payload /** @var array */ protected $bcc; + /** @var array */ + protected $attachments; + public static function forLoggedMail(string $loggedMail): self { $parser = new MailMimeParser(); @@ -38,6 +42,7 @@ public static function forLoggedMail(string $loggedMail): self // get the part in $loggedMail that starts with getHeader(HeaderConsts::TO)), self::convertHeaderToPersons($message->getHeader(HeaderConsts::CC)), self::convertHeaderToPersons($message->getHeader(HeaderConsts::BCC)), + $attachments, ); } @@ -55,7 +61,8 @@ public function __construct( ?string $subject = null, array $to = [], array $cc = [], - array $bcc = [] + array $bcc = [], + array $attachments = [] ) { $this->html = $html; $this->from = $from; @@ -63,6 +70,7 @@ public function __construct( $this->to = $to; $this->cc = $cc; $this->bcc = $bcc; + $this->attachments = $attachments; } protected static function getMailContent(string $loggedMail, IMessage $message): string @@ -76,6 +84,19 @@ protected static function getMailContent(string $loggedMail, IMessage $message): return substr($loggedMail, $startOfHtml) ?? ''; } + protected static function getMailAttachments(IMessage $message): array + { + return collect($message->getAllAttachmentParts()) + ->map(function (MimePart $attachmentPart) { + return [ + 'filename' => $attachmentPart->getFilename(), + 'content_id' => $attachmentPart->getContentId(), + 'content_type' => $attachmentPart->getContentType(), + 'content' => base64_encode($attachmentPart->getContent()), + ]; + })->toArray(); + } + public function getType(): string { return 'mailable'; @@ -90,6 +111,7 @@ public function getContent(): array 'to' => $this->to, 'cc' => $this->cc, 'bcc' => $this->bcc, + 'attachments' => $this->attachments, ]; } diff --git a/tests/TestClasses/TestMailable.php b/tests/TestClasses/TestMailable.php index df2fc5a..7644088 100644 --- a/tests/TestClasses/TestMailable.php +++ b/tests/TestClasses/TestMailable.php @@ -3,6 +3,7 @@ namespace Spatie\LaravelRay\Tests\TestClasses; use Illuminate\Mail\Mailable; +use Illuminate\Mail\Mailables\Attachment; class TestMailable extends Mailable { @@ -10,4 +11,17 @@ public function build() { return $this->markdown('mails.test'); } + + /** + * Get the attachments for the message. + * + * @return array + */ + public function attachments(): array + { + return [ + Attachment::fromData(fn () => 'file1')->as('file_1.txt')->withMime('text/plain'), + Attachment::fromData(fn () => 'file2')->as('file_2.txt')->withMime('text/plain'), + ]; + } } diff --git a/tests/Unit/MailableTest.php b/tests/Unit/MailableTest.php index 1a0b498..db13a83 100644 --- a/tests/Unit/MailableTest.php +++ b/tests/Unit/MailableTest.php @@ -17,6 +17,7 @@ ->send(new TestMailable()); expect($this->client->sentRequests())->toHaveCount(1); + expect($this->client->sentPayloads()[0]['content']['attachments'])->toHaveCount(2); }); it('can send multiple mailable payloads', function () { From ada754d97b24c4e207d16f969429d5bd35509d12 Mon Sep 17 00:00:00 2001 From: Rias Date: Thu, 11 Jul 2024 12:04:37 +0200 Subject: [PATCH 2/4] Update tests --- tests/Payloads/LoggedMailPayloadTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/Payloads/LoggedMailPayloadTest.php b/tests/Payloads/LoggedMailPayloadTest.php index bae1d4a..9f9a2df 100644 --- a/tests/Payloads/LoggedMailPayloadTest.php +++ b/tests/Payloads/LoggedMailPayloadTest.php @@ -54,6 +54,7 @@ 'email' => 'willem@spatie.be', ], ], + 'attachments' => [], ])->toEqual($payload->getContent()); }); @@ -85,5 +86,6 @@ ], 'cc' => [], 'bcc' => [], + 'attachments' => [], ])->toEqual($payload->getContent()); }); From 1d5f0ed19dd0fb4a66a986b726dbe25ef55346ac Mon Sep 17 00:00:00 2001 From: Rias Date: Thu, 11 Jul 2024 12:06:01 +0200 Subject: [PATCH 3/4] Fix compatibility with older versions --- tests/TestClasses/TestMailable.php | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/tests/TestClasses/TestMailable.php b/tests/TestClasses/TestMailable.php index 7644088..8281324 100644 --- a/tests/TestClasses/TestMailable.php +++ b/tests/TestClasses/TestMailable.php @@ -3,25 +3,13 @@ namespace Spatie\LaravelRay\Tests\TestClasses; use Illuminate\Mail\Mailable; -use Illuminate\Mail\Mailables\Attachment; class TestMailable extends Mailable { public function build() { - return $this->markdown('mails.test'); - } - - /** - * Get the attachments for the message. - * - * @return array - */ - public function attachments(): array - { - return [ - Attachment::fromData(fn () => 'file1')->as('file_1.txt')->withMime('text/plain'), - Attachment::fromData(fn () => 'file2')->as('file_2.txt')->withMime('text/plain'), - ]; + return $this->markdown('mails.test') + ->attachData('file1', 'file_1.txt') + ->attachData('file2', 'file_2.txt'); } } From 2aa71e1986a72c7ca3c40bb3322d6caa5ff970bd Mon Sep 17 00:00:00 2001 From: Rias Date: Thu, 11 Jul 2024 12:08:39 +0200 Subject: [PATCH 4/4] wip --- tests/Unit/MailableTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Unit/MailableTest.php b/tests/Unit/MailableTest.php index db13a83..1a0b498 100644 --- a/tests/Unit/MailableTest.php +++ b/tests/Unit/MailableTest.php @@ -17,7 +17,6 @@ ->send(new TestMailable()); expect($this->client->sentRequests())->toHaveCount(1); - expect($this->client->sentPayloads()[0]['content']['attachments'])->toHaveCount(2); }); it('can send multiple mailable payloads', function () {