如何将收件人电子邮件地址动态传递到 Mail::send() 内部回调函数?

How can I dinamically pass the recipient e-mail address into the Mail::send() inner callback function?

我在 PHPLaravel 中还很新,我发现一些困难的尝试向未预定义的用户发送电子邮件。

所以我正在使用 Illuminate\Support\Facades\Mail class.

如果我这样做,效果很好:

$registrationMail = [
    'title' => 'Registrazione albergatore su portale BeTrivius',
    'url' => URL::to('/').'/activate?email='.$data['email'].'&token='.$token,
    'email' => $data['email'],
    'name' => $data['name'],
    'surname' => $data['surname']
];

Mail::send('emails.registrationMail', $registrationMail, function($message) {
    $message->to('mail@gmail.com', 'recipient-name')->subject('email subject');
});

所以我调用 Mail class 的 send() 方法传递给它:

它工作正常,但以这种方式进行时,电子邮件收件人是静态的,并且电子邮件将始终发送到相同的电子邮件地址。

这对我不利,因为我必须将这封电子邮件发送到 $registrationMail['email'] 属性 中包含的地址.

但是这样做:

Mail::send('emails.registrationMail', $registrationMail, function($message) {

    $message->to($registrationMail['email'], $data['name'].' '.$data['surname'])->subject('titolo');
});

$registrationMail 似乎在回调函数中不可见(它对我说 Undefined variable)。为什么我在回调函数内部看不到外部 属性?

所以我无法通过这种方式检索此信息。

所以我尝试添加 $registrationMail 变量作为这个回调函数的参数,这样:

Mail::send('emails.registrationMail', $registrationMail, function($message, $registrationMail) {

    $message->to($registrationMail['email'], $registrationMail['name'].' '.$registrationMail['surname'])->subject('titolo');
});

以这种方式执行 $registrationMail 变量不是未定义的,但是当它尝试发送电子邮件时,我收到此错误消息:

ErrorException in RegistrationController.php line 90:
Missing argument 2 for App\Http\Controllers\RegistrationController::App\Http\Controllers\{closure}()
in RegistrationController.php line 90
at HandleExceptions->handleError(2, 'Missing argument 2 for App\Http\Controllers\RegistrationController::App\Http\Controllers\{closure}()', 'C:\xampp\htdocs\HotelRegistration\app\Http\Controllers\RegistrationController.php', 90, array('message' => object(Message))) in RegistrationController.php line 90
at RegistrationController->App\Http\Controllers\{closure}(object(Message))
at call_user_func(object(Closure), object(Message)) in Mailer.php line 199
at Mailer->send('emails.registrationMail', array('title' => 'Registrazione albergatore su portale BeTrivius', 'url' => 'http://laravel.dev/activate?email=nobili.andrea@gmail.com&token=4759d4ec56cf1e63447638e0d7a042e6', 'email' => 'nobili.andrea@gmail.com', 'name' => 'Andrea', 'surname' => 'Nobili'), object(Closure)) in Facade.php line 221
at Facade::__callStatic('send', array('emails.registrationMail', array('title' => 'Registrazione albergatore su portale BeTrivius', 'url' => 'http://laravel.dev/activate?email=nobili.andrea@gmail.com&token=4759d4ec56cf1e63447638e0d7a042e6', 'email' => 'nobili.andrea@gmail.com', 'name' => 'Andrea', 'surname' => 'Nobili'), object(Closure))) in RegistrationController.php line 93
at Mail::send('emails.registrationMail', array('title' => 'Registrazione albergatore su portale BeTrivius', 'url' => 'http://laravel.dev/activate?email=nobili.andrea@gmail.com&token=4759d4ec56cf1e63447638e0d7a042e6', 'email' => 'nobili.andrea@gmail.com', 'name' => 'Andrea', 'surname' => 'Nobili'), object(Closure)) in RegistrationController.php line 93
at RegistrationController->store(object(Request))
at call_user_func_array(array(object(RegistrationController), 'store'), array(object(Request))) in Controller.php line 55
at Controller->callAction('store', array(object(Request))) in ControllerDispatcher.php line 44

其中 第 90 行 是:

Mail::send('emails.registrationMail', $registrationMail, function($message, $registrationMail) {

所以问题似乎是我无法将第二个参数传递给内部回调函数。为什么?

如何将收件人电子邮件地址正确传递到此内部回调函数中?最聪明的方法是什么?

您需要将变量添加到函数的 use 部分以将其发送到同一范围。

例如:

Mail::send('emails.registrationMail', $registrationMail, function($message) use($registrationMail) {
    $message->to($registrationMail['email'])->subject('titolo');
});