I have an error in Laravel when I am sending an email.
I have a form with a select tag and when I select the user and click submit I need to send him a mail after I select it.
Here is my Controller method:
public function store() { $customerId = Input::get('customerid'); $invoice = New Invoice ; $invoice->customerid = Input::get('customerid'); $invoice->save(); if ($invoice->save()) { $email = Customer::whereRaw(' id = :customer',array('customer'=> $customerId ))->first(array('email')); Mail::send('invoices.mail', array($pinvoices,$unpinvoices), function($message) { $message->to($email , 'Name')->subject('your invoices '); }); } }
When I click submit I get an error :
Undefined variable: email
and when I tried to add my email as a static value like :
$email = "myemail@mail.com" ; Mail::send('invoices.mail', array($pinvoices,$unpinvoices), function($message){ $message->to($email , 'Name')->subject('your invoices '); });
It’s also getting the same error !!
and when i put the value like this :
Mail::send('invoices.mail', array($pinvoices,$unpinvoices), function($message){ $message->to("myemail@mail.com" , 'Name')->subject('your invoices '); });
it works perfectly !
Advertisement
Answer
Closures work just like a regular function. You need to inject your outer scope variables into function’s scope.
Mail::send('invoices.mail', array($pinvoices,$unpinvoices), function($message) use ($email) { $message->to($email , 'Name')->subject('your invoices '); });