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

heisabrutalo's avatar

Pass Variable to Markdown from Notification through Mailable

Hello :)

I have a problem with passing a Variable to Markdown from Notification through Mailable via AppServiceProvider. I think i overcomplicated it.

Im getting the error Undefined variable: maildata (View: message.blade.php)

I have to pass the $maildata (array) to the markdown.

I Override the VerifyEmail::toMailUsing Method in AppServiceProvider.php boot method (dont know if its needed)

AppServiceProvider.php

// Override the email notification for verifying email
            VerifyEmail::toMailUsing(function ($notifiable){
                $verifyUrl = URL::temporarySignedRoute(
                    'verification.verify',
                    Carbon::now()->addMinutes(Config::get('auth.verification.expire', 60)),
                    ['id' => $notifiable->getKey()]
                );
                $maildata = [
                    'mainTitle' => 'TEST'
                ];
                return new EmailVerification($verifyUrl, $notifiable, $maildata);
            });

EmailVerification.php Mailable

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class EmailVerification extends Mailable
{
    use Queueable, SerializesModels;

    public $verifyUrl;
    protected $user;
    public $maildata;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($maildata, $url, $user)
    {
        $this->maildata = $maildata;
        $this->verifyUrl = $url;
            $this->user = $user;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->markdown( 'mail.emailVerification', compact('maildata') );
    }
}

My message.blade.php

@component('mail::layout')
{{-- Header --}}
@slot('header')
@component('mail::header', ['maildata' => $maildata])
@endcomponent
@endslot

{{-- Body --}}
{{ $slot }}

{{-- Subcopy --}}
@isset($subcopy)
@slot('subcopy')
@component('mail::subcopy')
{{ $subcopy }}
@endcomponent
@endslot
@endisset

{{-- Footer --}}
@slot('footer')
@component('mail::footer')
@endcomponent

@endslot
@endcomponent

What am I doing wrong? I tried some different approaches witch compact, a new mailable, new notification. Nothing worked. I also checked the GitHub Issue https://github.com/laravel/framework/issues/18597

Nothing worked for me.

If i remove the maildata in the component the email sending works, just without the variable data :(

thx hope someone has an approach for me.

0 likes
32 replies
laracoft's avatar

@heisabrutalo

I think you got the variable order wrong, try

return new EmailVerification($maildata, $verifyUrl, $notifiable);	
heisabrutalo's avatar

ty i tried it, but didnt help :( I also tried this and changed everything according:

return new EmailVerification($maildata);
laracoft's avatar

@heisabrutalo

Your EmailVerification's __construct($maildata, $url, $user) takes 3 variables.

Your must call new EmailVerification(...) in exactly the same order.

What is the error you getting now?

laracoft's avatar

@heisabrutalo

public function build()
{
    $maildata = $this->maildata;
    return $this->markdown( 'mail.emailVerification', compact('maildata') );
}	
heisabrutalo's avatar

ty, yeah i changed that. Same Error: Undefined variable: maildata (View: messsage.blade.php)

I dont get the point, the email is working fine without the variable :(

laracoft's avatar

@heisabrutalo

public function build()
{
    // dd($this->maildata); // uncomment and show output if below does not work
    return $this->markdown('mail.emailVerification')
	->with('maildata', $this->maildata);
}
heisabrutalo's avatar

Same error :(

Also if i comment the inside of the whole build() function, it shows the same Error...

heisabrutalo's avatar

Sorry.

Undefined variable: maildata (View: E:\laragon\www\xxx\resources\views\vendor\mail\html\message.blade.php)

@component('mail::header', ['maildata' => $maildata])

laracoft's avatar

@heisabrutalo

We are jumping the gun, what do you see when

public function build()
{
    dd($this->maildata);
}
heisabrutalo's avatar

I am going crazy. Undefined variable: maildata ... Is it possible that the AppServiceProvider is something wrong?

public function boot()
    {
        Schema::defaultStringLength(191);

        view()->composer('*', function($view) {
            $view->with('user', Auth::user() );
        });

        // Override the email notification for verifying email
            VerifyEmail::toMailUsing(function ($notifiable){
                $verifyUrl = URL::temporarySignedRoute(
                    'verification.verify',
                    Carbon::now()->addMinutes(Config::get('auth.verification.expire', 60)),
                    ['id' => $notifiable->getKey()]
                );
                $maildata = [
                    'mainTitle' => 'TEST'
                ];
                return new EmailVerification($maildata);
            });

    }

maybe at the VerifyEmail::toMailUsing?

laracoft's avatar

@heisabrutalo

Why your return new not fixed?

return new EmailVerification($maildata, $verifyUrl, $notifiable);
laracoft's avatar

@heisabrutalo

please... you are not making sense

 public function __construct($maildata, $url, $user)
{
   ...
}
laracoft's avatar

@heisabrutalo

And can please show output of below... don't keep jumping the gun

public function build()
{
    dd($this->maildata);
}
heisabrutalo's avatar

I changed this long time ago.

dd is not working, it shows the same error. Undefined variable: maildata even if i comment out everything in this build method :( dont get it...

public $maildata;

public function __construct($maildata)
    {
        $this->maildata = $maildata;
    }
heisabrutalo's avatar

In the AppServiceProvider.php i override the VerifyEmail::toMailUsing Method

and i call the EmailVerification Mailalbe in the boot Method.

public function boot()
    {
        Schema::defaultStringLength(191);

        view()->composer('*', function($view) {
            $view->with('user', Auth::user() );
        });

        // Override the email notification for verifying email
            VerifyEmail::toMailUsing(function ($notifiable){
                $verifyUrl = URL::temporarySignedRoute(
                    'verification.verify',
                    Carbon::now()->addMinutes(Config::get('auth.verification.expire', 60)),
                    ['id' => $notifiable->getKey()]
                );
                $maildata = [
                    'mainTitle' => 'TEST'
                ];
                return new EmailVerification($maildata);
            });

    }
laracoft's avatar

@heisabrutalo

  1. boot() is only defining what to do, not triggering
  2. What is the URL you are using to trigger the email verification? By creating an account?
heisabrutalo's avatar

ive seen that from a tutorial... with the AppServiceProvider.php

yes registering, abc.com/register is the url

laracoft's avatar

@heisabrutalo

Comment out VerifyEmail:: in your boot() and test your EmailVerification first.

Create a totally new route and test it

Route::get('/testemail', function () {
    $maildata = [
        'mainTitle' => 'TEST'
    ];
    Mail::to('[email protected]')->send(new EmailVerification($maildata));
})
heisabrutalo's avatar

dd works

array:1 [▼
  "mainTitle" => "TEST"
]

so the mailable should be fine, right?

laracoft's avatar

@heisabrutalo

Ok, now try this in boot()

VerifyEmail::toMailUsing(function ($notifiable){
    $verifyUrl = URL::temporarySignedRoute(
        'verification.verify',
        Carbon::now()->addMinutes(Config::get('auth.verification.expire', 60)),
        ['id' => $notifiable->getKey()]
    );
    $maildata = [
        'mainTitle' => 'TEST'
    ];

    return (new MailMessage)->markdown('mail.emailVerification')->with('maildata', $maildata);
});
heisabrutalo's avatar

Same Error :(

Undefined variable: maildata (View: E:\laragon\www\xxx\resources\views\vendor\mail\html\message.blade.php)

at: @component('mail::header', ['maildata' => $maildata])

MichalOravec's avatar

@heisabrutalo Sent your data to the component in mail.emailVerification like

@component('mail::message', compact('maildata'))
heisabrutalo's avatar

Guys, it works! ty

I made the mistake to change the var maildata in component in vendor/mail/html/message.blade.php and not in the Mailable template....

This was so confusing with the AppServiceProvider, so i dont need it anymore?

heisabrutalo's avatar

Guys, im getting crazy. Again i cannot pass a Variable from the Notification to the Mailable, what am i doing? Is it me or sometimes we cannot mix classes and mailables and notifications?

toMail function in the Notification

public function toMail($notifiable)
    {
        $verificationUrl = $this->verificationUrl($notifiable);
        /*
        return (new MailMessage)
            ->subject(Lang::get('user.emailVerificationTitle'))
            ->action(Lang::get('user.emailVerification'), $verificationUrl)
            ->line(Lang::get('user.emailVerificationNotRequested'));
        */

        $maildata = [
            'mainTitle' => __('mail.confirmation'),
            'url' => $verificationUrl
        ];

        return (new EmailVerification)
            ->subject(Lang::get('user.emailVerificationTitle'))
            ->locale(session()->get('locale'))
            ->to($notifiable->email)
            ->with(['maildata', $maildata]);
    }

Mailable, im passing it right?

class EmailVerification extends Mailable
{
    use Queueable, SerializesModels;

    public $maildata;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($maildata)
    {
        $this->maildata = $maildata;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        dd($this->maildata);
        return $this->markdown( 'mail.emailVerification');
    }
}

Error: Too few arguments to function App\Mail\EmailVerification::__construct(), 0 passed 1 expected.

How can i pass data from Notification to Mailable? Sounds like the created array in the Notification is empty, but why?

ty

laracoft's avatar

@heisabrutalo

  1. PAY ATTENTION to your __construct()
  2. How many variables are there in __construct()? 1, 2 or 3?
  3. When you create a NEW instance return (new EmailVerification($maildata)), number of variables MUST match
        return (new EmailVerification($maildata))
            ->subject(Lang::get('user.emailVerificationTitle'))
            ->locale(session()->get('locale'))
            ->to($notifiable->email);
Next

Please or to participate in this conversation.