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

qbixx's avatar
Level 4

Use variables inside the Markdown Mailables

Hello,

I've created a new mailable by following the guide on: https://laravel.com/docs/5.4/mail#markdown-mailables

Inside the default layout, the name of the application is used inside the header of the mail. I would like to replace the application name with an image of the logo. Therefore i've exported the Markdown mail components with:

php artisan vendor:publish --tag=laravel-mail

Inside the view of the mail i've added a public property

public function __construct(Visit $visit)
{
    $this->visit = $visit;
}

This variable is working inside the Markdown file that is used to create the body of the email message.

However, I can't seem to pass the variable into the resources/views/vendor/mailfiles. I doesn't matter if i'm using the layout.blade.php or header.blade.php files.

After adding the $visitvariable inside one of those two files, i'm getting a Undefined variable: visit error.

Is there a different way to add variables to both the header and footer inside the Markdown template files?

0 likes
7 replies
qbixx's avatar
Level 4

Does anybody know how to get the variables working inside the Mardown template files?

scottlaurent's avatar

it's incredibly annoying that this is so undocumented.

I was trying to add an unsubscribe link in the footer based on if the user was subscribed to the given email... can't seem to get variables into the sub components.

  1. which comes first, markdown or html in the parsing? might be obvious, but you have to tinker with it to figure it out.

  2. you can pass variables but you can't get them deeper into the nested views. supposedly you can pass them something like @component('mail::message', ['testvar' => $testvar]) but this doesn't appear to work.

I've wasted an hour screwing with this and just giving up and going back to straight html templates.

the above solution isn't working for me.

clausche@gmail.com's avatar

Yes, i did it... First the Mailable Class

use Queueable, SerializesModels;

/**
 * Create a new message instance.
 *
 * @return void
 */
public function __construct() {

}

/**
 * Build the message.
 *
 * @return $this
 */
public function build(Request $request) {
    $email = $request->email;

    return $this->from($request->email)
        ->markdown('auth.mail.contactNew')
        ->with('email', $request->email);
}
}

Then... the Blade file

@component('mail::message')

Introduction

{{$email}}
The body of your message.

@component('mail::button', ['url' => ''])
Button Text
@endcomponent

Thanks,

{{ config('app.name') }}
@endcomponent
2 likes
Annette67's avatar

Is there a solution for this - I'm finding it impossible to pass any variables through to the footer or header using the @component('mail::footer', ['testvar' => $testvar]) suggestion in the docs in the blade template for the email. Keep getting an undefined variable error. Should say all the solutions don't seem to address the issue of headers and footers unless I'm missing something.

barnabas.kecskes's avatar

I was struggling with the same problem for a few minutes now just to realise that it is indeed available if you pass it on directly in your first entry point.

So, you create your mailable, where you specify the markdown template to be used (and make your variable public of course):

class Example extends Mailable
{
    public $variable;

    // ...
    
    public function build()
    {
        return $this->markdown('mail.example');
    }
}

Then you have something like this in your resources/views/mail/example.blade.php:

@component('mail::message')
// ...
@endcomponent

Instead, you should have this:

@component('mail::message', ['variable' => $variable])
// ...
@endcomponent

If you do that, the $variable will be available throughout your mail, including your footer and not just within the body. I hope it helps - it has solved the problem for me.

8 likes
Miguejarias's avatar

@BARNABAS.KECSKES - Ok i've been looking for a working solution for the last 10 minutes and this is by far the best and easiest. Thank you lol.

Please or to participate in this conversation.