Add (array) $data to all mail events#49
Merged
LukeTowers merged 5 commits intowintercms:developfrom Sep 27, 2021
Merged
Conversation
Sometimes it is useful to have all the data that is passed to the mail view within $data available in mail events, specifically mailer.send. It also allows passing along data without having to "abuse" the $message object by setting properties on it which are not related.
I recently had to log the output of mails that were sent along with other relevant data, that was only known within the Mail closure, so, not available within the Event listener.
A very simple example:
```php
// Plugin.php
public function boot()
{
// This is how the event listener signature works before this PR, whereas after the PR we would also be able to access $data
// After PR: \Event::listen('mailer.send', function ($mailer, $view, \Illuminate\Mail\Message $message, $data)
\Event::listen('mailer.send', function ($mailer, $view, \Illuminate\Mail\Message $message)
{
$log = new MailLog();
// We do not have access to $data here, because it is not passed to the listener
$log->transactionId = ?;
// With the PR
$log->transactionId = $data['transactionId'];
}
}
// My\Plugin\Jobs\SendInvoice.php
class SendInvoice
{
public function fire($job, $userId)
{
// ...
$transaction = $this->createTransaction($user);
$data = ['checkoutUrl' => $transaction->checkoutUrl, 'transactionId' => $transaction->id];
\Mail::send('my.plugin::mail.invoice', $data, function($message)
{
// ....
}
}
}
```
bennothommo
reviewed
Sep 27, 2021
LukeTowers
requested changes
Sep 27, 2021
LukeTowers
reviewed
Sep 27, 2021
LukeTowers
reviewed
Sep 27, 2021
LukeTowers
approved these changes
Sep 27, 2021
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Sometimes it is useful to have all the data that is passed to the mail view within $data available in mail events, specifically mailer.send. It also allows passing along data without having to "abuse" the $message object by setting properties on it which are not related.
I recently had to log the output of mails that were sent along with other relevant data, that was only known within the Mail closure, so, not available within the Event listener.
A very simple example: