dongxieting9623 2017-03-01 15:35
浏览 53
已采纳

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

I am pretty new in PHP and moreoer in Laravel and I am finding some difficult trying to send an e-mail to a not predefine user.

So I am using the Illuminate\Support\Facades\Mail class.

If I do something like this it works fine:

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

So I am calling the send() method of the Mail class passing to it:

  • The view representing the e-mail template.
  • An array containing some data used into the previous view.
  • A callback function in which is contained the logic to set the recipient e-mail address and the e-mail subject

It works fine but doning in this way the email recipient is static and the e-mail will be send always to the same e-mail address.

This is not good for me because I have to send this e-mail to the address contained into the $registrationMail['email'] property.

But doing in this way:

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

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

the $registrationMail seems to be not visible into the callback function (it says to me Undefined variable). Why I can't see an external property from the inside of a callback function?

So I can't retrieve this information in this way.

So I tryied to add the $registrationMail variable as a parameter of this callback function, in this way:

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

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

Doing in this way the $registrationMail variable is not undefined but when it try to send the e-mail I obtain this error message:

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

where line 90 is:

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

So the problem seems to be that I can't pass a second parameter to the inner callback function. Why?

How can I correctly pass the recipient e-mail address into this inner callback function? What is the smartest way to do it?

  • 写回答

1条回答 默认 最新

  • dongyang1518 2017-03-01 15:37
    关注

    You need to add the variable to the use part of your function to send it to the same scope.

    For example:

    Mail::send('emails.registrationMail', $registrationMail, function($message) use($registrationMail) {
        $message->to($registrationMail['email'])->subject('titolo');
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵