Laravel htmlspecialchars() expects parameter 1 to be string, object given in my project?
Asked Answered
R

4

11

So i'm trying to code a simple website form. But it has this htmlspecialchars error.

I've tried to make {{ $message }} but it didn't work. has the same error.

this is my controller :

<?php
namespace App\Http\Controllers;
use Mail;
use Illuminate\Http\Request;
class ContactMessageController extends Controller
{
public function create()
    {
        return view('form');
    }

public function store(Request $request)
{
    $this->validate($request, [
        'name' => 'required',
        'email' => 'required|email',
        'address' => 'required',
    ]);

    Mail::send('emails.contact-message', [
        'message' => $request->message
    ], function($mail) use($request) {
        $mail->from($request->email, $request->name);

        $mail->to('[email protected]')->subject('Contact message');
    });

        return redirect()->back()->with('flash_message', 'thanks');
    }
}

and this is my blade

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Customer Details</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="main.css" />
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.0/css/bootstrap.min.css" integrity="sha384-PDle/QlgIONtM1aqA2Qemk5gPOE7wFq8+Em+G/hmo5Iq0CCmYZLv3fVRDJ4MMwEA" crossorigin="anonymous">
<style>
    .invalid-feedback {
        display: block;
    }
</style>
</head>
<body>
<div class ="container">
    <h1>Customer Form</h1>
        @if (Session::has('flash_message'))
            <div class="alert alert-success">{{ Session::get('flash_message') }}</div>
        @endif
    <form method="post" action="{{ route('contact.store') }}">
    {{ csrf_field() }}
        <div class="form-group">
            <label>Full Name : </label>
            <input type="text" class="form-control" name="name">
            @if ($errors->has('name'))
                <small class="form-text invalid-feedback">{{ $errors->first('name') }}</small>
            @endif
        </div>

        <div class="form-group">
            <label>Email : </label>
            <input type="text" class="form-control" name="email">
            @if ($errors->has('email'))
                <small class="form-text invalid-feedback">{{ $errors->first('email') }}</small>
            @endif
        </div>

        <div class="form-group">
            <label>Address : </label>
            <textarea name="address" class ="form-control"></textarea>
            @if ($errors->has('address'))
                <small class="form-text invalid-feedback">{{ $errors->first('address') }}</small>
            @endif
        </div>

        <div class="form-group">
            <label>Message : </label>
            <textarea name="message" class ="form-control"></textarea>
            @if ($errors->has('message'))
                <small class="form-text invalid-feedback">{{ $errors->first('message') }}</small>
            @endif
        </div>

        <button class="btn btn-primary">Submit</button>


    </form>
</div>

</body>
</html>

and this is my contact-message.blade.php

{{ $message }}

also i've tried {{dd($message)}}

but it didnt work.

please help.

Rivas answered 12/2, 2019 at 8:2 Comment(9)
seen this? #40046420Barbarossa
yes, but it doesn't help either.Rivas
Have you tried checking what $message contains? Does the error even come from that specific variable?Gregarine
@NicoHaase can you please explain how do I check what $message contains? i'm a newbie in laravel..Rivas
Well, you could use var_dump, echo, maybe Symfony's dump works, more sophisticated approaches would start with XDebug - I don't want to be rude, and I don't want to sound arrogant, but that is basic craftmanship that you need to learn to get more efficient in programming. You will see that it pays out quickly :)Gregarine
@NicoHaase thanks, but i think i still can't understand....Rivas
Can you share your email.contact-message view?Guib
@rdhawladar it only contains {{ $message }}Rivas
@Rivas I have added an answer I hope it will work.Guib
G
38

Just change the array key from message to messages in your controller like below:

$data = array(
        'messages' => $request->message
        );

and also in the blade print it as {{$messages}}

A $message variable is always passed to e-mail views, and allows the inline embedding of attachments. So, it is best to avoid passing a message variable in your view payload.

Check the note in this link: http://laravel.com/docs/5.0/mail#basic-usage

Guib answered 12/2, 2019 at 10:35 Comment(4)
Welcome @Rivas . As you are new, just to let you know that if any answer works for you then you can accept it and accepting a perfect answer will add value to your profile. See details from here: stackoverflow.com/help/someone-answersGuib
Thanks! This applies to Laravel 5.8 and this entry about the $message was removed from the documentation. Not sure why...Exsiccate
They should somehow say these kinds of stuff in the debug page. I searched everywhere and tried many methods to send data. Oh my god.Mozambique
Thanks for this, I was banging my head against the screen until I found this post and realised some reserved word BS was going on. Sigh. Thanks!Plumbic
P
2

also I fetch the same problem Just change it message to messages it works for me.

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class ContactUserIncomingMail extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
     */

    public $name;
    public $email;
    public $phone;
    public $subject;
    public $message;

    public function __construct($name,$email,$phone,$subject,$message)
    {
        $this->name = $name;
        $this->email = $email;
        $this->phone = $phone;
        $this->message = $message;
        $this->subject = $subject;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        $name = $this->name;
        $email = $this->email;
        $phone = $this->phone;
        $messages = $this->message;
        $subject = $this->subject;
        $subject = 'Website Customer Query';
        return $this->view('Mail.contact-user-incomeing-mail',compact('name','email','phone','messages','subject'))->subject($subject);
    }
}
Paramatta answered 8/11, 2022 at 19:59 Comment(0)
P
0

To fix this issue don't use $message on the textfield, instead use something like "emailMessage" since using $message on the email template is somehow overridden by Laravel's default message handling.

Promiscuous answered 20/2 at 6:0 Comment(0)
D
-1

You should try this:

public function store(Request $request)
{
    $this->validate($request, [
        'name' => 'required',
        'email' => 'required|email',
        'address' => 'required',
    ]);

    $fromName = $request->name;
      $subject = "MailSent";
      $data = array(
        'message' => $request->message
        );

      $fromEmail = $request->email;

      $toName = 'test';
      $toEmail = '[email protected]';


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

        $message->from($fromEmail, $fromName);
        $message->to($toEmail, $toName);
        $message->subject($subject);
      });

        return redirect()->back()->with('flash_message', 'thanks');
    }
}
Deputize answered 12/2, 2019 at 8:14 Comment(4)
Please explain what exactly you've changed and why that makes a differenceGregarine
@NicoHaase: I have changed mail send functionalityDeputize
i've tried it, but it doesn't work either. still the same error.Rivas
What exactly have you changed and why do you think that this solves the problem? As a long-term user of SO, you should know that these explanations are crucial as the OP should be able to learn from your answerGregarine

© 2022 - 2024 — McMap. All rights reserved.