Ad
Ad
Ad

Laravel provides an easy interface for mail sending. One day, I am working with Laravel Mail function. Suddenly, I had some issues with mail function of Laravel. When I check the error log I found like “expects parameter 1 to be string, array given error exception”

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

So, Somewhere in my blade template, which I use “message” for the content. When I am trying to print “message” at that time I got the exception like

expects parameter 1 to be string, array given error exception

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

My code for sending mail looks like

Laravel Mail Code

$mailTemplate = "Welcome to the Laravelhive.";
Mail::send('emails.users', ['message' => $mailTemplate,'name' => 'Saroj'], function($message)
{
    $message->to('foo@example.com', 'John Smith')->subject('Welcome!');
});

Laravel Mail Blade template “users.blade.php” looks like

<p>Hello There,</p>

<ul>
    <li><strong>Name:</strong> {{ $name }}</li>
</ul>

<p>{{ $message }}</p>

When I check my code everything seems to look fine. As you can see my code everything looking fine. Finally, I got a solution after spending 5 hours. I made a silly mistake on my code.

The Solution

The solution is quite simple as I create a silly mistake on my code. It is also mention in 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.

As per Laravel Mail documentation $message variable is always passed to e-mail blade template. When I am trying to pass and access the $message variable from view/blade template.

Finally, I found the solution from the Laravel documentation.

The solution is We need to rename $message variable to $text and everything should work perfectly. Just rename the $message variable.