Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 = '[email protected]';
       $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.

like image 494
Mitch Avatar asked Jan 01 '26 20:01

Mitch


1 Answers

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.

like image 103
The Alpha Avatar answered Jan 03 '26 11:01

The Alpha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!