Programming code

Laravel Mail Solution For: Htmlentities() expects parameter 1 to be string, array given error exception

I love Laravel. Simple, elegant, easy, well documentated. However, sometimes the errors are not always very clear. Certainly when having issues while using ajax. Because when the system is unable to produce a slick stacktrace through the request, you will need to view the Laravel logs in BASE_PATH/storage/logs .

The Exception

In my case, I had some issues with the Mail function of Laravel. This is what I found ion my logs:

Next exception 'ErrorException' with message 'htmlentities() expects parameter 1 to be string, object given (View: BASE_BATH/resources/views/emails/contact.blade.php)' in BASE_BATH/vendor/laravel/framework/src/Illuminate/Support/helpers.php:469

 

So, somewhere in my blade view which I use for a contact message, a string, or rather, an unwanted object, is being parsed resulting in the following error:

Next exception 'ErrorException' with message 'htmlentities() expects parameter 1 to be string, object given

My code

But when checking, everything seems fine. To give an indication of what I used, check the code below:

<ul>
    <li><strong>Name:</strong> {{ $name }}</li>
    <li><strong>E-mail:</strong> {{ $email }}</li>
    <li><strong>Company:</strong> {{ $company }}</li>
    <li><strong>Phonenumber:</strong> {{ $phonenumber }}</li>
    <li><strong>IP:</strong> {{ $ip }}</li>
</ul>
<h2>Message:</h2>
<p>{{ $message }}</p>

As you might guess, everything seems fine, right? Well, obviously not, but why?

Solution

The solution is quite simple but rather silly. It is even partly mentioned as a warning at the Laravel Mail documentation:

Note: 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.

So of course the View cannot comprehend and escape $message . It isn’t the message YOU have passed to the view, it is the message instance used by Laravel.

Simply rename $message to $text and everything should work perfectly fine again!

Comments.

    • author Sergio
    • /
    • date September 27, 2017

    Very helpful article.

    Thanks

    • author Milad
    • /
    • date February 4, 2018

    Hi,
    Thanks, I have been hit my head on a wall for 12 hours what is wrong with my code, until finding your solution.

    • author Johnny
    • /
    • date February 6, 2018

    thank you so very much

    • author Liju
    • /
    • date February 12, 2018

    Thank You Very Helpfull

    • author Felipe
    • /
    • date March 22, 2018

    Idol! thank!

    • author Tamo
    • /
    • date May 12, 2018

    Thanks.

    • author Stef
    • /
    • date February 14, 2019

    Thank you! Saved me!

    • author Mercy
    • /
    • date May 11, 2019

    Thanks Allot! i’ve been turning round my codes checking for error and everything seems perfect until saw this article.. Thanks once more..

    • author Marcelo Leopold
    • /
    • date July 2, 2019

    Thanks a lot!

    • author Pizão
    • /
    • date July 10, 2019

    Great!
    Thanks for discovery and share. 🙂

    • author Gangadharan Nadar
    • /
    • date September 9, 2020

    Wow…I too was stuck in this problem….your solution worked wonders….Thanks Tim

Reply.