Reputation: 6199
I'm trying to pass a variable with the to email address into the Mail:send function and I'm getting the following error because it cannot read the variable within the function.
Undefined variable: to
$data = array(
"name"=>"Foo Bar"
);
$to = "[email protected]";
Mail::send('mail', $data, function ($message) {
$message->from('[email protected]', 'Name');
$message->subject("Subject Name");
$message->to($to);
});
How could i pass the email address into the send function as it won't be a static email address i would be sending emails to?
Upvotes: 1
Views: 996
Reputation: 2614
Because of the way PHP's anonymous functions work, you have to specifically indicate each variable that should be included in to the function's scope, using the use
keyword.
Mail::send('mail', $data, function ($message) use ($to) {
$message->from('[email protected]', 'Name');
$message->subject("Subject Name");
$message->to($to);
});
Here's a link to more information in PHP's documentation.
Upvotes: 4