Skip to content

Commit

Permalink
Adding message data to mail sending and sent events
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Brown committed Oct 24, 2017
1 parent ce7969c commit a24e0a8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
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

0 comments on commit a24e0a8

Please sign in to comment.