wannymiarelli's avatar

Laravel Mail - Undefined variable inside view

Laravel docs about Mail::send

Mail::send('emails.welcome', ['key' => 'value'], function($message)
{
    $message->to('foo@example.com', 'John Smith')->subject('Welcome!');
});

this is pretty clear, so i replaced " email.welcome " with my custom view (welcomemail.blade.php), this is the content :

<html>
<body>
<?php echo $key; ?>
</body>
</html>

now, if i try to send the email i get

Undefined variable: key (View: view_path)
0 likes
9 replies
mstnorris's avatar

@wannymiarelli Have you tried assigning a $data variable first and passing that through? You're right though, that is exactly what the docs say. Is your email being sent?

What version of Laravel are you using and on that note, are you using Homestead? What version of PHP are you using? Take a look at some Guidelines I put together about posting on here as an FYI :)

Check out http://stackoverflow.com/questions/27331782/laravel-mail-undefined-variable

$data = [
    'key' => 'value'
];

Mail::send('emails.welcome', $data, function($message)
{
    $message->to('foo@example.com', 'John Smith')->subject('Welcome!');
});
<html>
    <body>
        <?php echo $data['key']; ?>
    </body>
</html>
1 like
wannymiarelli's avatar

I will take a detailed look at the guilde-lines. The problem is solved, it was a conflict between two variables. Excuse me for this " useless " post.

Thanks for your time.

1 like
bashy's avatar

@mstnorris No need to do that and also $data isn't passed, you just use the index { { $key } } for example.

mstnorris's avatar

@wannymiarelli it isn't useless if the issue is solved. Please do bear in mind though, in the future when posting, please include all relevant code.

1 like
mstnorris's avatar

@bashy I know there wasn't any need but it is a better way in my opinion and it gets the OP to take another look at the code without just copying/pasting.

bashy's avatar

@mstnorris But it wouldn't work at all? Unless you did

$data = [
    'data' = [
        'key' => 'value',
    ],
];
wannymiarelli's avatar

@mstnorris There was no " copy / paste " this is all my code. Its pretty simple. It's just a controller action with this code

Mail::send('emails.welcome', ['key' => 'value'], function($message)
{
    $message->to('foo@example.com', 'John Smith')->subject('Welcome!');
});

And this is not written by me. Its just the DOC snippet ( and its supposed to work out of the box ).

There is no more code in my project, just this. I missed a variable conflict inside my view ( and this is my fault, i will look better next time :) )

@bashy are you sure this is the correct structure for the array ? Looked a lot before posting here and all did it like this

$data = ['key' => 'value'];

Thanks again fro your help, and excuse me for the confusion. Best Regards

bashy's avatar

@wannymiarelli My example isn't right, I was replying to mstnorris. No data variable is sent to the view for emails unless you'd add it as an array in the array sent to the view.

1 like
wannymiarelli's avatar

Oh ( damn me :) ). Yes this is correct, the variable passed inside the $data array can be accesses using the index.

Please or to participate in this conversation.