Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nckrtl's avatar

Notification toMail gives error "Trying to get property 'view' of non-object" when sending a markdown mailable instead of MailMessage

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'));
    }
}

0 likes
2 replies
nckrtl's avatar
nckrtl
OP
Best Answer
Level 2

Ah I found the problem. I misunderstood what a Mailable actually is.

I had to change use App\Mail\MailInspectionEntryResults;

to: use App\Mail\MailInspectionEntryResults as Mailable;

and in the toMail function I had to change: Mail::to($notifiable)->cc(...) to: return (new Mailable($this->inspection_entry, $this->inspection_name, $this->attachment_name))->to($notifiable->email)->cc($cc);

It is explained in the docs (https://laravel.com/docs/6.x/notifications#mail-notifications) but I must've overlooked it.

1 like
Istiake5's avatar

Seems like laravel cannot find your mail driver which you are trying to redirect in endpoint. The below code worked for your case:

public function via($notifiable)
    {
        return $this->toMail($notifiable);
    }
1 like

Please or to participate in this conversation.