Skip to content

Commit 5de1d43

Browse files
committed
Provide notification callback with Swift message
1 parent 3fb0d75 commit 5de1d43

3 files changed

Lines changed: 50 additions & 0 deletions

File tree

src/Illuminate/Notifications/Channels/MailChannel.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ protected function buildMessage($mailMessage, $notifiable, $notification, $messa
137137
if (! is_null($message->priority)) {
138138
$mailMessage->setPriority($message->priority);
139139
}
140+
141+
$this->runCallbacks($mailMessage, $message);
140142
}
141143

142144
/**
@@ -225,4 +227,20 @@ protected function addAttachments($mailMessage, $message)
225227
$mailMessage->attachData($attachment['data'], $attachment['name'], $attachment['options']);
226228
}
227229
}
230+
231+
/**
232+
* Run the callbacks for the message.
233+
*
234+
* @param \Illuminate\Mail\Message $mailMessage
235+
* @param \Illuminate\Notifications\Messages\MailMessage $message
236+
* @return $this
237+
*/
238+
protected function runCallbacks($mailMessage, $message)
239+
{
240+
foreach ($message->callbacks as $callback) {
241+
$callback($mailMessage->getSwiftMessage());
242+
}
243+
244+
return $this;
245+
}
228246
}

src/Illuminate/Notifications/Messages/MailMessage.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ class MailMessage extends SimpleMessage implements Renderable
7373
*/
7474
public $rawAttachments = [];
7575

76+
/**
77+
* The callbacks for the message.
78+
*
79+
* @var array
80+
*/
81+
public $callbacks = [];
82+
7683
/**
7784
* Priority level of the message.
7885
*
@@ -224,6 +231,19 @@ public function attachData($data, $name, array $options = [])
224231
return $this;
225232
}
226233

234+
/**
235+
* Add a callback for the message.
236+
*
237+
* @param callable $callback
238+
* @return $this
239+
*/
240+
public function withSwiftMessage($callback)
241+
{
242+
$this->callbacks[] = $callback;
243+
244+
return $this;
245+
}
246+
227247
/**
228248
* Set the priority of this message.
229249
*

tests/Notifications/NotificationMailMessageTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,16 @@ public function testReplyToIsSetCorrectly()
7474

7575
$this->assertSame([['test@example.com', null], ['test@example.com', 'Test']], $message->replyTo);
7676
}
77+
78+
public function testCallbackIsSetCorrectly()
79+
{
80+
$callback = function () {
81+
//
82+
};
83+
84+
$message = new MailMessage;
85+
$message->withSwiftMessage($callback);
86+
87+
$this->assertSame([$callback], $message->callbacks);
88+
}
7789
}

0 commit comments

Comments
 (0)