From d8b2c8b44d72069288ed8a2f7c9480cd7303fba2 Mon Sep 17 00:00:00 2001 From: Luke Kuzmish Date: Wed, 7 Jun 2023 22:55:23 -0400 Subject: [PATCH 1/4] add base64 encoding when serializing SentMessage --- src/Illuminate/Mail/Events/MessageSent.php | 21 ++++++---------- src/Illuminate/Mail/SentMessage.php | 28 ++++++++++++++++++++++ 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/src/Illuminate/Mail/Events/MessageSent.php b/src/Illuminate/Mail/Events/MessageSent.php index c482e07383cc..4eec25e2fdfc 100644 --- a/src/Illuminate/Mail/Events/MessageSent.php +++ b/src/Illuminate/Mail/Events/MessageSent.php @@ -46,14 +46,10 @@ public function __serialize() { $hasAttachments = collect($this->message->getAttachments())->isNotEmpty(); - return $hasAttachments ? [ - 'sent' => base64_encode(serialize($this->sent)), - 'data' => base64_encode(serialize($this->data)), - 'hasAttachments' => true, - ] : [ + return [ 'sent' => $this->sent, - 'data' => $this->data, - 'hasAttachments' => false, + 'data' => $hasAttachments ? base64_encode(serialize($this->data)) : $this->data, + 'hasAttachments' => $hasAttachments, ]; } @@ -65,13 +61,10 @@ public function __serialize() */ public function __unserialize(array $data) { - if (isset($data['hasAttachments']) && $data['hasAttachments'] === true) { - $this->sent = unserialize(base64_decode($data['sent'])); - $this->data = unserialize(base64_decode($data['data'])); - } else { - $this->sent = $data['sent']; - $this->data = $data['data']; - } + $this->sent = $data['sent']; + $this->data = (($data['hasAttachments'] ?? false) === true) + ? unserialize(base64_decode($data['data'])) + : $data['data']; } /** diff --git a/src/Illuminate/Mail/SentMessage.php b/src/Illuminate/Mail/SentMessage.php index cce42dbd009f..d36d9d5ab95b 100644 --- a/src/Illuminate/Mail/SentMessage.php +++ b/src/Illuminate/Mail/SentMessage.php @@ -51,4 +51,32 @@ public function __call($method, $parameters) { return $this->forwardCallTo($this->sentMessage, $method, $parameters); } + + /** + * Get the serializable representation of the object. + * + * @return array + */ + public function __serialize() + { + $hasAttachments = collect($this->sentMessage->getOriginalMessage()->getAttachments())->isNotEmpty(); + + return [ + 'hasAttachments' => $hasAttachments, + 'sentMessage' => $hasAttachments ? base64_encode(serialize($this->sentMessage)) : $this->sentMessage, + ]; + } + + /** + * Marshal the object from its serialized data. + * + * @param array $data + * @return void + */ + public function __unserialize(array $data) + { + $hasAttachments = (isset($data['hasAttachments']) && $data['hasAttachments'] === true); + + $this->sentMessage = $hasAttachments ? unserialize(base64_decode($data['sentMessage'])) : $data['sentMessage']; + } } From 10fdea2f5627b18bf8ea01d520f04c423047dfd1 Mon Sep 17 00:00:00 2001 From: Luke Kuzmish <42181698+cosmastech@users.noreply.github.com> Date: Thu, 8 Jun 2023 07:13:11 -0400 Subject: [PATCH 2/4] slim down expression --- src/Illuminate/Mail/SentMessage.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Mail/SentMessage.php b/src/Illuminate/Mail/SentMessage.php index d36d9d5ab95b..5a2a5f6b7096 100644 --- a/src/Illuminate/Mail/SentMessage.php +++ b/src/Illuminate/Mail/SentMessage.php @@ -75,7 +75,7 @@ public function __serialize() */ public function __unserialize(array $data) { - $hasAttachments = (isset($data['hasAttachments']) && $data['hasAttachments'] === true); + $hasAttachments = ($data['hasAttachments'] ?? false) === true; $this->sentMessage = $hasAttachments ? unserialize(base64_decode($data['sentMessage'])) : $data['sentMessage']; } From d01869bfab04383a26d2416896362b989b05515e Mon Sep 17 00:00:00 2001 From: Luke Kuzmish Date: Fri, 9 Jun 2023 14:08:11 -0400 Subject: [PATCH 3/4] adds test case --- .../Mail/Fixtures/blank_document.pdf | Bin 0 -> 777 bytes .../Integration/Mail/SentMessageMailTest.php | 78 ++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 tests/Integration/Mail/Fixtures/blank_document.pdf create mode 100644 tests/Integration/Mail/SentMessageMailTest.php diff --git a/tests/Integration/Mail/Fixtures/blank_document.pdf b/tests/Integration/Mail/Fixtures/blank_document.pdf new file mode 100644 index 0000000000000000000000000000000000000000..88f49e24ccb2cb30392b03ce2dcce0d999d1678c GIT binary patch literal 777 zcmZWn%Wm5+5WMp%_F^D8Bou8uQXtU5PGT5sT1U#w$bpg8sv1dbNeXcLoBe_wi~dA+ z725)A>agTsb~Q8PWPSH=rLIFU`Tg(jAE6)s^ZHHP-pc3NHwYK6jqcu{G)3QH=P!j^ zTT}K0TDZvHYAvZBTBTxG8dFijohck-*p+C}UZ$y_El-?{lM4+>$oV74&&Ia88A;(t z`h;EOzrszJrr^9q-45acxMGdThba`T_#v*7h>igqDuD|4%a%h?U@IY)GpL+k-VG^L zK@@{Uu@z5(vY8u`V5K*kblFw#BDy3+;7dC^+`+PXDsAxTP=?5gZr9 zQ8bHS3r9?m7GBUDv1vkXPB6NQb1Wo*C%iB|$0C)CJa2W~pyijF*T0a|$;=oJ?2Q6k Zbz4IZ;yo?rwe^FGD5jS`x&QJY{sYks&D;P0 literal 0 HcmV?d00001 diff --git a/tests/Integration/Mail/SentMessageMailTest.php b/tests/Integration/Mail/SentMessageMailTest.php new file mode 100644 index 000000000000..18c7f728eb67 --- /dev/null +++ b/tests/Integration/Mail/SentMessageMailTest.php @@ -0,0 +1,78 @@ +increments('id'); + }); + } + + public function testDispatchesNotificationSent() + { + $notificationWasSent = false; + + $user = SentMessageUser::create(); + + Event::listen( + NotificationSent::class, + function(NotificationSent $notification) use (&$notificationWasSent, $user) { + $notificationWasSent = true; + /** + * Confirm that NotificationSent can be serialized/unserialized as + * will happen if the listener implements ShouldQueue. + */ + /** @var NotificationSent $afterSerialization */ + $afterSerialization = unserialize(serialize($notification)); + + $this->assertTrue($user->is($afterSerialization->notifiable)); + + $this->assertEqualsCanonicalizing($notification->notification, $afterSerialization->notification); + }); + + $user->notify(new SentMessageMailNotification()); + + $this->assertTrue($notificationWasSent); + } +} + +class SentMessageUser extends Model +{ + use Notifiable; + + public $timestamps = false; +} + + +class SentMessageMailNotification extends Notification +{ + public function via(): array + { + return ['mail']; + } + + public function toMail(object $notifiable): MailMessage + { + return (new MailMessage) + ->line('Example notification with attachment.') + ->attach(__DIR__ . '/Fixtures/blank_document.pdf', [ + 'as' => 'blank_document.pdf', + 'mime' => 'application/pdf', + ]); + } +} From 6e8cb4beb4f2a68dc0cc8280b7d0a157b339b76e Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 13 Jun 2023 22:50:54 +0200 Subject: [PATCH 4/4] Update MessageSent.php --- src/Illuminate/Mail/Events/MessageSent.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Illuminate/Mail/Events/MessageSent.php b/src/Illuminate/Mail/Events/MessageSent.php index 4eec25e2fdfc..493e7d4380a0 100644 --- a/src/Illuminate/Mail/Events/MessageSent.php +++ b/src/Illuminate/Mail/Events/MessageSent.php @@ -62,6 +62,7 @@ public function __serialize() public function __unserialize(array $data) { $this->sent = $data['sent']; + $this->data = (($data['hasAttachments'] ?? false) === true) ? unserialize(base64_decode($data['data'])) : $data['data'];