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

落花浮王杯 提交于 2019-12-04 19:56:36

问题


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.


回答1:


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');
});


来源:https://stackoverflow.com/questions/16809254/defining-the-email-address-for-the-mailsend-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!