Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non-initialized public typed properties on mailables throw an exception #49812

Closed
j3j5 opened this issue Jan 24, 2024 · 0 comments · Fixed by #49813
Closed

Non-initialized public typed properties on mailables throw an exception #49812

j3j5 opened this issue Jan 24, 2024 · 0 comments · Fixed by #49813

Comments

@j3j5
Copy link
Contributor

j3j5 commented Jan 24, 2024

Laravel Version

10.42.0

PHP Version

8.2.7

Database Driver & Version

No response

Description

When using Mailables, all public properties are made available to the view, that's expected behavior. But if the mailable contains a public (typed) variable that hasn't been initialized, the buildViewData() method throws an exception
Typed property App\Mail\YourClass::$variable must not be accessed before initialization.

I think there should be a check on buildViewData() to make sure typed variables are initialized before being passed to the view.

Steps To Reproduce

php artisan make:mail OrderShipped

<?php

namespace App\Mail;

use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;

class OrderShipped extends Mailable
{
    public User $user;

    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     */
    public function __construct()
    {
        //
    }

Then, try to send this mailable, from tinker:

$mail = $mail = new App\Mail\OrderShipped();
Mail::to("mail@example.org")->send($mail);

It will throw an exception

Error Typed property App\Mail\OrderShipped::$user must not be accessed before initialization.

@driesvints driesvints changed the title Non-initialized public typed properties on mailables throw an exception. Non-initialized public typed properties on mailables throw an exception Jan 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant