0

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.

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

0

You have to pass your $userEmail variable to the closure scope by using use.

function($message) use ($userEmail)

For more infos using anonymous functions take a look here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.