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.6] Adding message data to mail sending and sent events #21804

Merged
merged 1 commit into from
Oct 25, 2017
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
13 changes: 11 additions & 2 deletions src/Illuminate/Mail/Events/MessageSending.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@

class MessageSending
{
/**
* The message data.
*
* @var array
*/
public $data;

/**
* The Swift message instance.
*
Expand All @@ -14,11 +21,13 @@ class MessageSending
/**
* Create a new event instance.
*
* @param \Swift_Message $message
* @param \Swift_Message $message
* @param array $data
* @return void
*/
public function __construct($message)
public function __construct($message, $data = [])
{
$this->message = $message;
$this->data = $data;
}
}
11 changes: 10 additions & 1 deletion src/Illuminate/Mail/Events/MessageSent.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@

class MessageSent
{
/**
* The message data.
*
* @var array
*/
public $data;

/**
* The Swift message instance.
*
Expand All @@ -15,10 +22,12 @@ class MessageSent
* Create a new event instance.
*
* @param \Swift_Message $message
* @param array $data
* @return void
*/
public function __construct($message)
public function __construct($message, $data = [])
{
$this->message = $message;
$this->data = $data;
}
}
14 changes: 8 additions & 6 deletions src/Illuminate/Mail/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,10 @@ public function send($view, array $data = [], $callback = null)
// its recipients. We will then fire the sent event for the sent message.
$swiftMessage = $message->getSwiftMessage();

if ($this->shouldSendMessage($swiftMessage)) {
if ($this->shouldSendMessage($swiftMessage, $data)) {
$this->sendSwiftMessage($swiftMessage);

$this->dispatchSentEvent($message);
$this->dispatchSentEvent($message, $data);
}
}

Expand Down Expand Up @@ -458,30 +458,32 @@ protected function sendSwiftMessage($message)
* Determines if the message can be sent.
*
* @param \Swift_Message $message
* @param array $data
* @return bool
*/
protected function shouldSendMessage($message)
protected function shouldSendMessage($message, $data = [])
{
if (! $this->events) {
return true;
}

return $this->events->until(
new Events\MessageSending($message)
new Events\MessageSending($message, $data)
) !== false;
}

/**
* Dispatch the message sent event.
*
* @param \Illuminate\Mail\Message $message
* @param array $data
* @return void
*/
protected function dispatchSentEvent($message)
protected function dispatchSentEvent($message, $data = [])
{
if ($this->events) {
$this->events->dispatch(
new Events\MessageSent($message->getSwiftMessage())
new Events\MessageSent($message->getSwiftMessage(), $data)
);
}
}
Expand Down