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');
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 comsol稳态求解器 找不到解,奇异矩阵有1个空方程返回的解不收敛。没有返回所有参数步长;pid控制
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功