简体   繁体   中英

Laravel 4 contact form using mail class, error undefined $data from view containing email message

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM