Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel On-Demand Notification with CC

Tags:

php

laravel

I'm sending notifications using the facade and it works well for addressing multiple 'to' emails however I need to be able to CC someone else in.

Notification::route('mail', ['[email protected]', '[email protected]'])->notify(new InvoicePaid($invoice));

I am hoping to address user1, and cc user2. Is it possible with the notification facade?


2 Answers

I suppose a solution (not as elegant as I would want), is to include the cc into the array that is passed and then define it in the MailMessage().

$data = array('name' => 'User Name', 'count' => 6, 'body' => 'My str', 'cc' = > '[email protected]');

On the Notification

public function toMail($notifiable)
    {
        $cc = $this->data['cc'];

return (new MailMessage)
                    ->cc($cc)



You can pass the user to be cc-ed through the constructor. Let's call it $user2:

$user1->notify(new InvoicePaid($invoice, $user2));

In your notification:

    public function __construct($invoice, User $user)
    {
        $this->user = $user;
    }

    public function toMail($notifiable)
    {
        return (new MailMessage)
                    ->cc($this->user->email)
                    ...
    }
like image 41
maelga Avatar answered Nov 24 '25 10:11

maelga



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!