Skip to content

Commit

Permalink
[9.x] Sent message alternative (#40963)
Browse files Browse the repository at this point in the history
* Use SentMessage instead of original message in event

* wip

* Alternative MessageSent

* Dynamically get the original message

* wip

* formatting

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
driesvints and taylorotwell committed Feb 11, 2022
1 parent 3a98b49 commit 63ca843
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
43 changes: 32 additions & 11 deletions src/Illuminate/Mail/Events/MessageSent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@

namespace Illuminate\Mail\Events;

use Symfony\Component\Mime\Email;
use Exception;
use Illuminate\Mail\SentMessage;

/**
* @property \Symfony\Component\Mime\Email $message
*/
class MessageSent
{
/**
* The Symfony Email instance.
* The message that was sent.
*
* @var \Symfony\Component\Mime\Email
* @var \Illuminate\Mail\SentMessage
*/
public $message;
public $sent;

/**
* The message data.
Expand All @@ -23,14 +27,14 @@ class MessageSent
/**
* Create a new event instance.
*
* @param \Symfony\Component\Mime\Email $message
* @param \Illuminate\Mail\SentMessage $message
* @param array $data
* @return void
*/
public function __construct(Email $message, array $data = [])
public function __construct(SentMessage $message, array $data = [])
{
$this->sent = $message;
$this->data = $data;
$this->message = $message;
}

/**
Expand All @@ -43,11 +47,11 @@ public function __serialize()
$hasAttachments = collect($this->message->getAttachments())->isNotEmpty();

return $hasAttachments ? [
'message' => base64_encode(serialize($this->message)),
'sent' => base64_encode(serialize($this->sent)),
'data' => base64_encode(serialize($this->data)),
'hasAttachments' => true,
] : [
'message' => $this->message,
'sent' => $this->sent,
'data' => $this->data,
'hasAttachments' => false,
];
Expand All @@ -62,11 +66,28 @@ public function __serialize()
public function __unserialize(array $data)
{
if (isset($data['hasAttachments']) && $data['hasAttachments'] === true) {
$this->message = unserialize(base64_decode($data['message']));
$this->sent = unserialize(base64_decode($data['sent']));
$this->data = unserialize(base64_decode($data['data']));
} else {
$this->message = $data['message'];
$this->sent = $data['sent'];
$this->data = $data['data'];
}
}

/**
* Dynamically get the original message.
*
* @param string $key
* @return mixed
*
* @throws \Exception
*/
public function __get($key)
{
if ($key === 'message') {
return $this->sent->getOriginalMessage();
}

throw new Exception('Unable to access undefined property on '.__CLASS__.': '.$key);
}
}
14 changes: 9 additions & 5 deletions src/Illuminate/Mail/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,11 +282,15 @@ public function send($view, array $data = [], $callback = null)
$symfonyMessage = $message->getSymfonyMessage();

if ($this->shouldSendMessage($symfonyMessage, $data)) {
$sentMessage = $this->sendSymfonyMessage($symfonyMessage);
$symfonySentMessage = $this->sendSymfonyMessage($symfonyMessage);

$this->dispatchSentEvent($message, $data);
if ($symfonySentMessage) {
$sentMessage = new SentMessage($symfonySentMessage);

return $sentMessage === null ? null : new SentMessage($sentMessage);
$this->dispatchSentEvent($sentMessage, $data);

return $sentMessage;
}
}
}

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

0 comments on commit 63ca843

Please sign in to comment.