Apologies if I'm missing something here as I'm new to Laravel but when I send a closure to the Mail::send() method to define the mail recipient, it works fine if the email address is available the global scope, like this:
Mail::send('frontend.emails.default', $data, function($message)
{
$message->to(Input::get('email'))->subject('Hi');
});
But how can I pass a value in that's scoped to the calling method? For example:
$user = User::find($id);
Mail::send('frontend.emails.default', $data, function($message)
{
$message->to($user->email)->subject('Hi');
});
I tried adding it to the $data array but that's used in the view and isn't available in the callback.
Thanks for your help.
There's a little documented feature in PHP that allows you to pass variables from the current scope into a closure. In short, you need to use ($user)
...
$user = User::find($id);
Mail::send('frontend.emails.default', $data, function($message) use ($user)
{
$message->to($user->email)->subject('Hi');
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With