Laravel 4 contact form using mail class, error undefined $ submission data containing email

I am trying to use the Laravel Mail class for the first time and am experiencing some difficulties. When I try to send a contact form to a 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 email.contact

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

I am confused why the variable is $datanot passed to the view. I get this error: Undefined variable: dataThanks for any help or ideas.

+3
source share
1 answer

Change next line

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

to that

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

In Viewyou can use

Message: {{ $msg }}

: message data View.

+3

All Articles