Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.8] Provide notification callback with Swift message #28535

Merged
merged 1 commit into from
May 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/Illuminate/Notifications/Channels/MailChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ protected function buildMessage($mailMessage, $notifiable, $notification, $messa
if (! is_null($message->priority)) {
$mailMessage->setPriority($message->priority);
}

$this->runCallbacks($mailMessage, $message);
}

/**
Expand Down Expand Up @@ -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;
}
}
20 changes: 20 additions & 0 deletions src/Illuminate/Notifications/Messages/MailMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down
12 changes: 12 additions & 0 deletions tests/Notifications/NotificationMailMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,16 @@ public function testReplyToIsSetCorrectly()

$this->assertSame([['[email protected]', null], ['[email protected]', 'Test']], $message->replyTo);
}

public function testCallbackIsSetCorrectly()
{
$callback = function () {
//
};

$message = new MailMessage;
$message->withSwiftMessage($callback);

$this->assertSame([$callback], $message->callbacks);
}
}