Laravel – send Email by Notification

fmchanprogrammingLeave a Comment

First we create a trait for user. Visit https://medium.com/@kshitij206/traits-in-laravel-5db8beffbcc3 (Traits in PHP and Laravel) if you want to learn trait.

Also we make a function in the trait to call the notification event.

trait SendEmail {
    public function sendEmailNotification($name) {
        $this->notify(new EmailNotification($name));
    }
}
class User {
    use SendEmail,

send a email inside the notification

class EmailNotification extends Notification {
    use Queueable;

    public $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function toMail($notifiable) {
        return (new MailMessage())->line($this->name);
    }
}

Finally, send a email with variable by a user

$user->sendEmailNotification('abc');

And you can also change the mail template

vendor/mail/html
notification/email.blade.php

Leave a Reply

Your email address will not be published. Required fields are marked *