Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining the email address for the Mail::send() method

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.

like image 700
Russ Back Avatar asked May 29 '13 08:05

Russ Back


1 Answers

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');
});
like image 124
Phill Sparks Avatar answered Oct 20 '22 17:10

Phill Sparks