0
votes

I'm making a contact form with laravel 5.4 and I want to get an email when someone sends the contact form. I'm using Mailtrap to receive emails.

The problem I'm getting is that I get this error when I submit the form.

ErrorException in helpers.php line 533: htmlspecialchars() expects parameter 1 to be string, object given (View: C:\xampp\htdocs\website\app\Modules\Templates\Resources\Views\emails\contact.blade.php)

My contact function

public function contact()
{
    $data = Input::all();

    $rules = array(
        'name' => '',
        'email' => '',
        'message' => '',
    );

    $validator = Validator::make($data, $rules);

    if($validator->passes())
    {
        Mail::send('templates::emails.contact', $data, function($message){
            $message->from(Input::get('email'), Input::get('name'));
            $message->to('[email protected]', 'Info')->subject('Testing contact form');
        });

        Session::flash('success', 'Your message has been sent successfully.');
        return back();
    }else{
        return back()->withErrors($validator);
    }
}

and my contact.blade.php that is the information that gets sent to me

<h1>We been contacted by.... </h1>

{{ $name }}<br />
{{ $email }}<br />
{{ $subject }}<br />
{{ $message }}<br />
2
Can you debug the $data? Add dd($data); before the validation in the contact function. - Jerodev
you need to save the variables that you are passing to the view, else direct by Input the blade wont get it - Exprator
@Jerodev - When I debug $data I get the information that I entered in my form which is array:5 [▼ "_token" => "mI35WN4IGUlefCi4XXT80nhKcM2Cc3esosFtUTKX" "name" => "test name" "email" => "[email protected]" "subject" => "test" "message" => "drgrg" ] - Shiva478
@Exprator - Isn't that what the $data does in the line Mail::send(('templates::emails.contact', $data, function($message) It grabs the data and passes it to the contact.blade.php - Shiva478
@Shiva478 then you need to pass $data['name'] instead of $name - Exprator

2 Answers

0
votes

You're passing the $data which holds the array of input. You need to access them like shown below.

Change the data being passed to

Mail::send('templates::emails.contact', compact('data'), function($message)

Change your view code to

{{ $data['name'] }}<br/>
{{ $data['email'] }}<br/>
{{ $data['message'] }}<br/>

You're also trying to access subject in the view when isn't available in the input.

0
votes

you need to Add $subject in ur cantact function or remove it from cantact.blade.php, try this code :

public function contact()
{
    $cdata = Input::all();

    $crules = array(
        'name' => '',
        'email' => '',
        'subject' => '',
        'message' => '',
    );

    $validator = Validator::make($cdata, $crules);

    if($validator->passes())
    {
        Mail::send('templates::emails.contact', $cdata, function($message){
            $message->from(Input::get('email'), Input::get('name'));
            $message->to('[email protected]', 'Info')->subject('Testing contact form');
        });

        Session::flash('success', 'Your message has been sent successfully.');
        return back();
    }else{
        return back()->withErrors($validator);
    }
}

and some times u got an error because ur variables have the same name of stored variables ( like data,timer,for ... etc)