Issue
This is weird. I am sure I am missing something simple. I have the following code:
$productToken = Input::get('key');
$products = new Product;
$userEmail = $products->activateProduct($productToken);
$productDetailsArray = $products->getProductDetailsForUserEmail($productToken);
$emailData = $productDetailsArray;
Mail::send('emails.productReleased', $emailData, function($message){
$message->to($userEmail)->subject('Notification - Your Product was Released to the Public!');
});
It is supposed to activate the product in the database and then send email to user. The email of the user is in the $userEmail and when I did var_dump, it shows. Somehow this line throws an error that $userEmail is undefined:
$message->to($userEmail)->subject('Notification - Your Product was Released to the Public!');
This is the error I get:
Undefined variable: userEmail
I used Mail function before but instead of passing variable I passed the Input::get('email') because it was in a registration form. Right now, I don't have access to the Input but rather $userEmail. Please advise.
Solution
You are using a callback so function so at the time the function is called, the userEmail
variable is certainly out of scope. You should send the userEmail
variable to the function, maybe like this :
Mail::send('emails.productReleased', $emailData, function($message) use ($userEmail) {
$message->to($userEmail)->subject('Notification - Your Product was Released to the Public!');
});
See https://www.php.net/manual/functions.anonymous.php#example-191 for information about lambda (anonymous) function and context.
Answered By - Holt
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.