0

no idea why this is happening.

Heres a basic code I am testing:

<?php

// get each user and send an email
$query = DB::table('users')->where('email', '[email protected]')->get();

foreach ($query as $user) {
  $email = $user->email;
  $data = array();
  Mail::send('emails.wereback', $data, function($message)
  {
    $message->from('[email protected]', 'BuildSanctuary');
    $message->to($email);
    $message->subject("We are back online!");
  });
}

?>

This is giving me an error of undefined variable '$email'.

The problem is, if I dont try to send an email and instead just echo out the $email variable it works perfectly fine...

1 Answer 1

3

You have to import your $email variable into the closure's scope with the use keyword:

Mail::send('emails.wereback', $data, function($message) use ($email)
{
    $message->from('[email protected]', 'BuildSanctuary');
    $message->to($email);
    $message->subject("We are back online!");
});
Sign up to request clarification or add additional context in comments.

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.