phplaravellaravel-6

Laravel send email to (notify) user using another email?


Some users have another email address that is stored as secondary_email column in users table.

How to send Notification using $user->notify() to this secondary email address?


Solution

  • You would override the toMail method of your notification. For example:

    use App\Mail\InvoicePaid as Mailable;
    
    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return Mailable
     */
    public function toMail($notifiable)
    {
        return (new Mailable($this->invoice))
               ->to($this->user->secondary_email ?? $this->user->email);
    }
    

    You can read more about formatting notifications here.