Framework: $message variable not available in mail templates

Created on 4 May 2017  ·  12Comments  ·  Source: laravel/framework

  • Laravel Version: 5.4.21
  • PHP Version: 7.1.0
  • Database Driver & Version: MySQL 5.7.16

Description:

The Laravel documentation for the Mail component states:

Laravel automatically makes the $message variable available to all of your email templates

However, this is not the case. This variable is not defined.

It seems that the $message variable was made available in templates using the “old” mailer method (https://github.com/laravel/framework/blob/5.4/src/Illuminate/Mail/Mailer.php#L195), whereas Mailables don’t include the $message variable when building up its view data (https://github.com/laravel/framework/blob/5.4/src/Illuminate/Mail/Mailable.php#L204).

Steps To Reproduce:

  1. Create a Mailable.
  2. Define the template: $this->markdown('emails.test');
  3. In resources/views/emails/test.blade.php, try and reference $message.
  4. Receive ErrorException.

Most helpful comment

Just went to check this, and disappointed it’s been closed.

_Why_ is the $message variable not available in Markdown emails? I’d expect the same functionality (and variables) whether I was using Blade or Markdown. They’re Mailables, not two different components.

It would be nice to get parity between Blade and Markdown emails rather than put a message in the documentation about this inconsistency, as we all know not every person reads the documentation word for word 👀

All 12 comments

A $message variable is still available in Mailer views.

class TestMail extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('welcome');
    }
}

Please show me how to replicate.

@themsaid I added steps to reproduce.

Inside of my Markdown mail templates, if I try and use the $message variable I get an error saying the variable’s undefined.

Confirmed.

Any workaround available?

I have a very similar problem described here with Laravel 5.3, PHP 7.0 : https://laracasts.com/discuss/channels/laravel/can-not-assign-varialbes-in-mailable

Yes the $message variable is not available for markdown emails, that's for regular email views not markdown ones.

@themsaid I feel there should be a big, blinking marquee banner in the docs stating __$message is only for HTML views__, because I've been trying to figure this out for the past hour now 😭

I've seen a few similar issues like this, so I dare to say there's a need for this. Can we expect this to be implemented anytime in the future? Markdown templates provide great benefits, so this addition would be welcomed. It would bring markdown templates up to par with HTML views too.

Just went to check this, and disappointed it’s been closed.

_Why_ is the $message variable not available in Markdown emails? I’d expect the same functionality (and variables) whether I was using Blade or Markdown. They’re Mailables, not two different components.

It would be nice to get parity between Blade and Markdown emails rather than put a message in the documentation about this inconsistency, as we all know not every person reads the documentation word for word 👀

  1. any news of this coming in a future version?
    I assume a lot of people are using markdown for their email templates

The markdown part was actually the confusing part about this whole thing. It concerns plain-text messages, not markdown in general. You generally don't want to put attachments in your plain-text emails, that's why the $message variable isn't available there and only on the html views. I've sent in a PR to the docs which adds a bit more clarification to the note which was added in the meantime: https://github.com/laravel/docs/pull/4981

The markdown directory has been renamed to text for 5.8 to further prevent confusion.

@driesvints I'm actually trying to do it in the html template, in the header blade file, not in the markdown blade. Still, $message is undefined there. Is it supposed to work in those templates?

@pixelplant yes it should be available in extended templates.

So in short we there is no way that we can use $message variable in markdown ?

Was this page helpful?
0 / 5 - 0 ratings