I'm using Laravel Notifications and trying to send an email via the toMail
method. There is no real logic in my notification.
Below is the class.
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use App\Models\EmailContentModel;
use App\Traits\NotificationContentOverrides;
class EmailVerification extends Notification
{
use Queueable;
use NotificationContentOverrides;
private $url;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct(String $url)
{
$this->url = $url;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)->view('emails.dynamic');
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
This errors with:
Trying to get property 'view' of non-object
Coming from:
/vendor/laravel/framework/src/Illuminate/Notifications/Channels/MailChannel.php:92
According to the docs I should be able to do what I'm doing, which is returning a MailMessage
with a view.
My view is very simple. It's just a file that contains a string of Hello
for testing purposes.
Things I've ruled out:
dd((new MailMessage)->view('emails.dynamic')->render());
outputs the expected string so it isn't a problem with the view or path.
$notifiable
is a user model. I can see an email
attribute in there. It's all correct.
I've changed my email settings to log
to rule that out.
Looking at vendor/laravel/framework/src/Illuminate/Notifications/Channels/MailChannel.php I'm not sure if this whole thing is expecting some markdown. I don't want to use markdown, I want to use a HTML email/view. Is this the case?
I can create a Mailable class for each of my notifications (php artisan make:mail
) but I'm unsure if that's required? According to the docs it isn't.