From 5de1d43c72b1f9d6161def6a6c343c9bf663fbac Mon Sep 17 00:00:00 2001 From: Dwight Watson Date: Wed, 15 May 2019 17:48:05 +1000 Subject: [PATCH] Provide notification callback with Swift message --- .../Notifications/Channels/MailChannel.php | 18 +++++++++++++++++ .../Notifications/Messages/MailMessage.php | 20 +++++++++++++++++++ .../NotificationMailMessageTest.php | 12 +++++++++++ 3 files changed, 50 insertions(+) diff --git a/src/Illuminate/Notifications/Channels/MailChannel.php b/src/Illuminate/Notifications/Channels/MailChannel.php index f26319dd4652..df8d86634c3c 100644 --- a/src/Illuminate/Notifications/Channels/MailChannel.php +++ b/src/Illuminate/Notifications/Channels/MailChannel.php @@ -137,6 +137,8 @@ protected function buildMessage($mailMessage, $notifiable, $notification, $messa if (! is_null($message->priority)) { $mailMessage->setPriority($message->priority); } + + $this->runCallbacks($mailMessage, $message); } /** @@ -225,4 +227,20 @@ protected function addAttachments($mailMessage, $message) $mailMessage->attachData($attachment['data'], $attachment['name'], $attachment['options']); } } + + /** + * Run the callbacks for the message. + * + * @param \Illuminate\Mail\Message $mailMessage + * @param \Illuminate\Notifications\Messages\MailMessage $message + * @return $this + */ + protected function runCallbacks($mailMessage, $message) + { + foreach ($message->callbacks as $callback) { + $callback($mailMessage->getSwiftMessage()); + } + + return $this; + } } diff --git a/src/Illuminate/Notifications/Messages/MailMessage.php b/src/Illuminate/Notifications/Messages/MailMessage.php index dfbf0efc77ca..8b075af8802a 100644 --- a/src/Illuminate/Notifications/Messages/MailMessage.php +++ b/src/Illuminate/Notifications/Messages/MailMessage.php @@ -73,6 +73,13 @@ class MailMessage extends SimpleMessage implements Renderable */ public $rawAttachments = []; + /** + * The callbacks for the message. + * + * @var array + */ + public $callbacks = []; + /** * Priority level of the message. * @@ -224,6 +231,19 @@ public function attachData($data, $name, array $options = []) return $this; } + /** + * Add a callback for the message. + * + * @param callable $callback + * @return $this + */ + public function withSwiftMessage($callback) + { + $this->callbacks[] = $callback; + + return $this; + } + /** * Set the priority of this message. * diff --git a/tests/Notifications/NotificationMailMessageTest.php b/tests/Notifications/NotificationMailMessageTest.php index 8f8a0878a678..5336ba95533b 100644 --- a/tests/Notifications/NotificationMailMessageTest.php +++ b/tests/Notifications/NotificationMailMessageTest.php @@ -74,4 +74,16 @@ public function testReplyToIsSetCorrectly() $this->assertSame([['test@example.com', null], ['test@example.com', 'Test']], $message->replyTo); } + + public function testCallbackIsSetCorrectly() + { + $callback = function () { + // + }; + + $message = new MailMessage; + $message->withSwiftMessage($callback); + + $this->assertSame([$callback], $message->callbacks); + } }