Skip to content

Add (array) $data to all mail events#49

Merged
LukeTowers merged 5 commits intowintercms:developfrom
datune:patch-1
Sep 27, 2021
Merged

Add (array) $data to all mail events#49
LukeTowers merged 5 commits intowintercms:developfrom
datune:patch-1

Conversation

@datune
Copy link
Copy Markdown
Contributor

@datune datune commented Sep 25, 2021

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:

// 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) 
        {
            ....
        }
    }
}

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) 
        {
            // ....
        }
    }
}



```
@LukeTowers LukeTowers added this to the v1.1.7 milestone Sep 27, 2021
@LukeTowers LukeTowers merged commit 58d70ab into wintercms:develop Sep 27, 2021
LukeTowers added a commit that referenced this pull request Nov 13, 2021
* develop:
  Add create:settings command (#37)
  Display generated files in generator commands (#52)
  Add (array) $data to all mail events (#49)
  fix typo after Luke's last cleanup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants