Search code examples
phplaravelnotificationslaravel-5.3laravel-5.4

Laravel mail notification


With Laravel 5.3, I can define recipient in a Notification:

// In a class extending Illuminate\Notifications\Notification :

public function toMail($notifiable)
{
    return (new MailMessage)->line('hello')->to('[email protected]');
}

Since Laravel 5.4 (relevant commit), I can't use to. How can I update my code? I need to send a notification to an email which is not bound to a user nor an object. How to "hack" these missing functionality?


Solution

  • Create a minimal class with an email property:

    class MyNotifiable
    {
        use \Illuminate\Notifications\Notifiable;
    
        public $email;
    
        public function __construct($email)
        {
            $this->email = $email;
        }
    }
    

    Then call notify on your minimal class:

    (new MyNotifiable('[email protected]'))->notify(new MyNotification);
    

    And it works.