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 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算