I'm trying to send an email through Laravel, but I'm getting this error:
Undefined variable: contactEmail
Even though it got defined above it. What is going wrong here?
Controller
$contactName = Input::get('name'); $contactEmail = Input::get('email'); $contactMessage = Input::get('message'); $data = array('name'=>$contactName, 'email'=>$contactEmail, 'message'=>$contactMessage); Mail::send('template.mail', $data, function($message) { $message->from($contactEmail, $contactName); $message->to('[email protected]', 'myName')->subject('Mail via aallouch.com'); });
EDIT:
template.mail
Name: {{$name}} Email: {{$email}} Message:{{$message}}
As your $data variable is defined as:
$data = array( 'name'=>$contactName, 'email'=>$contactEmail, 'message'=>$contactMessage );
You won't have a $data available in your view, but you can use directly:
{{ $name }} {{ $email }} {{ $message }}
EDIT:
And your controller should have:
$contactName = Input::get('name'); $contactEmail = Input::get('email'); $contactMessage = Input::get('message'); $data = array('name'=>$contactName, 'email'=>$contactEmail, 'message'=>$contactMessage); Mail::send('template.mail', $data, function($message) use ($contactEmail, $contactName) { $message->from($contactEmail, $contactName); $message->to('[email protected]', 'myName')->subject('Mail via aallouch.com'); });
You must pass your variables to the closure using
use ($contactEmail, $contactName)
As shown above.
I have got this error and I have solved it. I have replace $message
keyword with $comment
from $data.
Example below :
$data = array('name' => 'vikas', 'message' => 'test message');
view
{{ $name }} {{ $message }}
It's getting error
$data = array('name' => 'vikas', 'comment' => 'test message'); {{ $name }} {{ $comment }}
Now it's working fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With