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 = "[email protected]" ;
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("[email protected]" , 'Name')->subject('your invoices ');
});
it works perfectly !
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 ');
});