From aed06b10f44b24928aeffa589f630bf5b5c19ed8 Mon Sep 17 00:00:00 2001 From: Giulio Troccoli-Allard Date: Wed, 10 Jul 2019 16:54:53 +0100 Subject: [PATCH] Add ability to set theme for mail notifications We already can set a different theme than the default one for our Mailable. It would be very useful if there was a way to set the theme for the mail notifications too. --- .../Notifications/Channels/MailChannel.php | 4 ++++ .../Notifications/Messages/MailMessage.php | 20 +++++++++++++++++++ .../SendingMailNotificationsTest.php | 4 ++++ 3 files changed, 28 insertions(+) diff --git a/src/Illuminate/Notifications/Channels/MailChannel.php b/src/Illuminate/Notifications/Channels/MailChannel.php index df8d86634c3c..e37931cf0f86 100644 --- a/src/Illuminate/Notifications/Channels/MailChannel.php +++ b/src/Illuminate/Notifications/Channels/MailChannel.php @@ -93,6 +93,10 @@ protected function buildView($message) return $message->view; } + if (property_exists($message, 'theme') && ! is_null($message->theme)) { + $this->markdown->theme($message->theme); + } + return [ 'html' => $this->markdown->render($message->markdown, $message->data()), 'text' => $this->markdown->renderText($message->markdown, $message->data()), diff --git a/src/Illuminate/Notifications/Messages/MailMessage.php b/src/Illuminate/Notifications/Messages/MailMessage.php index dd91420afaa9..7ae89c6a1815 100644 --- a/src/Illuminate/Notifications/Messages/MailMessage.php +++ b/src/Illuminate/Notifications/Messages/MailMessage.php @@ -31,6 +31,13 @@ class MailMessage extends SimpleMessage implements Renderable */ public $markdown = 'notifications::email'; + /** + * The current theme being used when generating emails. + * + * @var string|null + */ + public $theme = 'default'; + /** * The "from" information for the message. * @@ -134,6 +141,19 @@ public function template($template) return $this; } + /** + * Set the theme to use with the markdown template. + * + * @param string $theme + * @return $this + */ + public function theme($theme) + { + $this->theme = $theme; + + return $this; + } + /** * Set the from address for the mail message. * diff --git a/tests/Integration/Notifications/SendingMailNotificationsTest.php b/tests/Integration/Notifications/SendingMailNotificationsTest.php index 2141a09ff4ff..3cfd9142acee 100644 --- a/tests/Integration/Notifications/SendingMailNotificationsTest.php +++ b/tests/Integration/Notifications/SendingMailNotificationsTest.php @@ -74,6 +74,7 @@ public function test_mail_is_sent() 'email' => 'taylor@laravel.com', ]); + $this->markdown->shouldReceive('theme')->once()->andReturnSelf(); $this->markdown->shouldReceive('render')->once()->andReturn('htmlContent'); $this->markdown->shouldReceive('renderText')->once()->andReturn('textContent'); @@ -118,6 +119,7 @@ public function test_mail_is_sent_to_named_address() 'name' => 'Taylor Otwell', ]); + $this->markdown->shouldReceive('theme')->once()->andReturnSelf(); $this->markdown->shouldReceive('render')->once()->andReturn('htmlContent'); $this->markdown->shouldReceive('renderText')->once()->andReturn('textContent'); @@ -161,6 +163,7 @@ public function test_mail_is_sent_with_subject() 'email' => 'taylor@laravel.com', ]); + $this->markdown->shouldReceive('theme')->once()->andReturnSelf(); $this->markdown->shouldReceive('render')->once()->andReturn('htmlContent'); $this->markdown->shouldReceive('renderText')->once()->andReturn('textContent'); @@ -194,6 +197,7 @@ public function test_mail_is_sent_to_multiple_adresses() 'email' => 'taylor@laravel.com', ]); + $this->markdown->shouldReceive('theme')->once()->andReturnSelf(); $this->markdown->shouldReceive('render')->once()->andReturn('htmlContent'); $this->markdown->shouldReceive('renderText')->once()->andReturn('textContent');