I have a notification that I'd like to email to the authenticated user and some additional recipients via CC. As you can't add CC recipients to a MailMessage it's advised to return a mailable in the toMail method of the Notification instead of a MailMessage (https://github.com/laravel/ideas/issues/1046).
So I set up a Notification that returns a mailable in the toMail() and the email gets sent correctly. However, the Notification itself returns an error "Trying to get property 'view' of non-object".
In the mailable I'm not returning $this->view but $this->markdown as I want to send a markdown email. This is where I think it's going wrong. I think the notification expects that toMail returns a mailable with a view property, but as it sends the email via markdown I don't think there is a view property.
Is there any way to make the toMail aware of the (markdown) view?
I hope someone can point me in the right direction.
Notification (left out some methods, to keep this post as small as possible):
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use App\Mail\MailInspectionEntryResults;
use Illuminate\Support\Facades\Mail;
class SendInspectionEntryResults extends Notification implements ShouldQueue
{
use Queueable;
protected $inspection_entry;
protected $inspection_name;
protected $attachment_name;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct(InspectionEntry $inspection_entry, $inspection_name, $attachment_name)
{
$this->inspection_name = $inspection_name;
$this->inspection_entry = $inspection_entry;
$this->attachment_name = $attachment_name;
}
/**
* 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)
{
$cc = $this->getCcRecipients($notifiable->email, $this->inspection_entry);
return Mail::to($notifiable)
->cc($cc)
->queue(new MailInspectionEntryResults($this->inspection_entry, $this->inspection_name, $this->attachment_name));
}
Mailable:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use App\InspectionEntry;
class MailInspectionEntryResults extends Mailable
{
use Queueable, SerializesModels;
protected $inspection_name;
protected $inspection_entry;
protected $attachment_name;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct(InspectionEntry $inspection_entry, $inspection_name, $attachment_name)
{
$this->inspection_name = $inspection_name;
$this->inspection_entry = $inspection_entry;
$this->attachment_name = $attachment_name;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->markdown('vendor.notifications.email')
->subject('Inspectie '. $this->inspection_name .' afgerond: '.$this->inspection_entry->location_name)->with([
'level' => 'primary',
'greeting' => 'Inspectie '. $this->inspection_name .' afgerond: '.$this->inspection_entry->location_name,
'introLines' => ["Het inspectierapport is bijgevoegd als bijlage. Om overige gegevens zoals eventuele foto's en de status van opvolgacties te bekijken, moet u inloggen op de beveiligde omgeving."],
'actionText' => 'Bekijk inspectie',
'outroLines' => [],
'actionUrl' => route('inspections.show', ['id' => $this->inspection_entry->id]),
'salutation' => 'Dit is een automatisch gegenereerd bericht',
'displayableActionUrl' => route('inspections.show', ['id' => $this->inspection_entry->id]),
'displayableActionText' => route('inspections.show', ['id' => $this->inspection_entry->id])
])->attach(\Storage::path($this->attachment_name.'.pdf'));
}
}