Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel htmlspecialchars() error when sending email

I'm trying to get emails to send via Sendgrid in Laravel. I've been following Sendgrid's Knowledge Base Article on Sending Email with Laravel

I have everything set up in my .env file (though I had to also update the MAIL_HOST in ~/config/mail.php) and I am able to send a test email (which I'm doing from within a blade).

The blade for the email template itself is exactly the same as per SendGrid's documentation:

<!DOCTYPE html>
<html lang="en-US">
    <head>
        <meta charset="utf-8">
    </head>
    <body>
        <h2>Test Email</h2>
        <p>{{$message}}</p>
    </body>
</html>

The code I'm running in a separate blade (I'm just doing this for testing) is:

@php

use App\Mail\TestEmail;

$data = ['message' => 'Oh, hai!'];

Mail::to('[email protected]')->send(new TestEmail($data));

@endphp

With the above I get the following error:

htmlspecialchars() expects parameter 1 to be string, object given 
    (View: ~/resources/views/emails/test.blade.php)

When I don't use {{ message }} in my blade, then things are fine.

Could someone please let me know what's going on and how to fix it? This is my first time using Laravel and anything that would point me in the right direction is OK.

like image 491
Ryan Dorn Avatar asked Oct 21 '25 12:10

Ryan Dorn


1 Answers

$message is a reserved variable since Laravel 5.6, representing a Illuminate\Mail\Message object.

Replace $message with something like $content or $msg in your Blade template and in your PHP code, and everything will work!

like image 196
HeySora Avatar answered Oct 24 '25 03:10

HeySora



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!