定义Mail :: send()方法的电子邮件地址 [英] Defining the email address for the Mail::send() method
问题描述
很抱歉,如果我是Laravel的新手,但我在这里错过了一些东西,但是当我向Mail :: send()方法发送闭包以定义邮件收件人时,如果该电子邮件地址在全球范围内可用,则可以很好地工作范围,像这样:
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');
});
我尝试将其添加到$ data数组中,但这在视图中使用,在回调中不可用.
I tried adding it to the $data array but that's used in the view and isn't available in the callback.
感谢您的帮助.
推荐答案
PHP中有一个已记录的小功能,可让您将变量从当前作用域传递到闭包中.简而言之,您需要use ($user)
...
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');
});
这篇关于定义Mail :: send()方法的电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!