创建帐户后无法访问 PHP Laravel 5 用户类属性以发送电子邮件

标签 php email laravel laravel-5 account

我有一个问题,我无法将变量放入 laravel 的 Mail::send() 函数中。请看下面的代码:

$first_name = $request->input('first_name'),
$email = $request->input('email'),

//Create account

User::create([
    'first_name' => $first_name,
    'last_name' => $request->input('last_name'),
    'email' => $email,
    'password' => bcrypt($request->input('password')),
]);

//Send email to user

Mail::send('emails.test', ['fname' => $first_name], function($message)
{
    $message->to($email)
            ->subject('Welcome!');
});

return redirect()
    ->route('home')
    ->with('info', 'Your account has been created and an authentication link has been sent to the email address that you provided. Please go to your email inbox and click on the link in order to complete the registration.');

由于某种原因,代码在发送电子邮件时中断,因为我收到错误并将数据发送到数据库。为什么此后无法再访问该变量?

如有任何帮助,我们将不胜感激。谢谢

最佳答案

Closures may also inherit variables from the parent scope. Any such variables must be passed to the use language construct.

来源:http://php.net/manual/en/functions.anonymous.php

换句话说,$email 必须像这样继承:

Mail::send('emails.test', ['fname' => $first_name], function($message) use ($email)
{
    $message->to($email)
            ->subject('Welcome!');
});

注意:在第一行使用($email)

关于创建帐户后无法访问 PHP Laravel 5 用户类属性以发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33457624/

相关文章:

php - 通过 PHP mail() 函数发送短信时出现 'From' header 问题

mysql - Laravel 按日期排序,空日期总是在最后

php - PDO 替代 mysql_ 获取字段信息

php - 在左连接上显示 AVG

javascript - 无法使用 php 将 json 数据传递给 typeahead

php - 导出为 csv 时,货币英镑显示为 £

jquery clone() 不做任何事情

php - 获取数组的一部分

java - 在我的代码中实现电子邮件服务

php - 样式联系表会产生一封电子邮件