我在 PHP 中相当新,而在 Laravel 中更多,我发现在尝试向非预定义用户发送电子邮件时遇到了一些困难。
所以我使用的是 Illuminate \ Support \ Facades \ Mail 类。
如果我这样做,它可以正常工作:
$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 类的 send()方法:
它工作正常,但以这种方式捐赠电子邮件收件人是静态的,电子邮件将始终发送到同一个电子邮件地址。
这对我不利,因为我必须将此电子邮件发送到 $ registrationMail [' email'] 属性中包含的地址。
但是这样做:
Mail::send('emails.registrationMail', $registrationMail, function($message) {
$message->to($registrationMail['email'], $data['name'].' '.$data['surname'])->subject('titolo');
});
$ registrationMail 似乎在回调函数中不可见(它告诉我未定义变量)。为什么我无法从回调函数内部看到外部属性?
所以我无法以这种方式检索这些信息。
所以我试图以这种方式添加 $ 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) {
所以问题似乎是我无法将第二个参数传递给内部回调函数。为什么呢?
如何正确地将收件人电子邮件地址传递给此内部回调函数?最聪明的方法是什么?
答案 0 :(得分:0)
您需要将变量添加到函数的use
部分,以将其发送到同一范围。
例如:
Mail::send('emails.registrationMail', $registrationMail, function($message) use($registrationMail) {
$message->to($registrationMail['email'])->subject('titolo');
});