0
votes

I'm trying to create a contact form in Laravel using Laravel 5.3, but I get this nasty error here:

ErrorException in helpers.php line 519: htmlspecialchars() expects parameter 1 to be string, object given (View: /Applications/XAMPP/xamppfiles/htdocs/meps/resources/views/emails/contactemail.blade.php)

Here are the files that I was using:

The contact form

<div class="contact-form">
    <form class="margin-clear" role="form" action="{{ url('/sendmail') }}" method="POST">

    {{ csrf_field() }}
        <div class="form-group has-feedback">
            <label for="name">Name*</label>
            <input type="text" class="form-control" id="name" name="name" placeholder="">
            <i class="fa fa-user form-control-feedback"></i>
        </div>
        <div class="form-group has-feedback">
            <label for="email">Email*</label>
            <input type="email" class="form-control" id="email" name="email" placeholder="">
            <i class="fa fa-envelope form-control-feedback"></i>
        </div>
        <div class="form-group has-feedback">
            <label for="subject">Subject*</label>
            <input type="text" class="form-control" id="subject" name="subject" placeholder="">
            <i class="fa fa-navicon form-control-feedback"></i>
        </div>
        <div class="form-group has-feedback">
            <label for="message">Message*</label>
            <textarea class="form-control" rows="6" id="message" name="message" placeholder=""></textarea>
            <i class="fa fa-pencil form-control-feedback"></i>
        </div>
        <input type="submit" value="Submit" class="btn btn-primary">
    </form>
</div>

The Controller function

public function sendmail(Request $request, Mailer $mail) {

    $mail->to('[email protected]')->send(new ContactEmail($request->name, $request->email, $request->subject, $request->message));

    $request->session()->flash('mail-sent', 'Your email has been sent.');

    return redirect('/contact');

}

The Mailable class

class ContactEmail extends Mailable
    {
    use Queueable, SerializesModels;

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

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($name, $email, $subject, $message)
    {
        $this->name = $name;
        $this->email = $email;
        $this->subject = $subject;
        $this->message = $message;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->from($this->email)->view('emails.contactemail');
    }
}

And here is the route

Route::post('sendmail', 'EmailController@sendmail');
2
Looks like you have an object wrapped in {{ }} somewhere in your contactemail.blade.php file. You only want to put strings in there (or an object with a __toString magic method). - user1669496
This is the actual email template contactemail.blade.php - Kaley36

2 Answers

0
votes

You probably looking at the wrong view. The error points to

/views/emails/contactemail.blade.php

But this view has a <form> unless you are sending back to your users a form via e-mail, this looks a lot more like your contact form view and not your e-mail view. Something like:

/views/contact.blade.php (or whatever you have in there as a form)

As for the error, you must have a {{ $variable or functionCall() }} which is not receiving a string, but an object.

0
votes

I figured it out. The issue was with the variable $message, Laravel wrapped it in an actual object called Message. All I had to do was change the variable name to $theMessage and it worked find.