Question

I'm trying to use Laravel's Mail class for the first time and am having some difficulties. When attempting to submit the contact form to the mail class, I get an undefined variable error.

Controller

public function store()
{
    $validation = new Services\Validators\Contact;

    if($validation->passes()) {

       $fromEmail = Input::get('email');
       $fromName = Input::get('name');
       $subject = "Email from user at website.com";
       $data = Input::get('message');

       $toEmail = 'test@dummyemail.com';
       $toName = 'Mitch Glenn';

       Mail::send('emails.contact', $data, function($message) use ($toEmail, $toName, $fromEmail, $fromName, $subject){

           $message->to($toEmail, $toName);

           $message->from($fromEmail, $fromName);

           $message->subject($subject);
       });

    return Redirect::to('/')
        ->with('message', 'Your message was successfully sent!');
    }

    return Redirect::back()
        ->withInput()
        ->withErrors($validation->errors);
}

View emails.contact

<html>
   <body>
       Message: {{ $data->message }}
   </body>
</html>

I am confused why the $data variable isn't being passed to the view. I am getting this error: Undefined variable: data Thanks for any help or insights.

Was it helpful?

Solution

Change following line

$data = Input::get('message');

to this

$data = [ 'msg' => Input::get('message') ];

In your View you can use

Message: {{ $msg }}

Update: Don't use message as the variable name when passing data to the View.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top