blablabla blablabla blablabla blablabla -4 years ago 132
PHP Question

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

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?

Answer Source

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');
});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download