Prak
Prak

Reputation: 825

Unable to send email using sendgrid with Laravel 5.4 and found the error

I am new to sendgrid, and want to integrate sendgrid with Laravel. Here I tried as - Added below code in app\Mail\SendgridEmail.php

namespace App\Mail;

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

class SendgridEmail extends Mailable
{
use Queueable, SerializesModels;

public $data;

public function __construct($data)
{
    $this->data = $data;
}

public function build()
{
    $address = '[email protected]';
    $subject = 'This is a demo!';
    $name = 'Sam';

    return $this->view('emails.templateUserRegister')
                ->from($address, $name)                    
                ->subject($subject)
                ->with([ 'message' => $this->data['message'] ]);

}
}

- Created template file views/emails/templateUserRegister.blade.php as

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

- Added below code to controller

use App\Mail\SendgridEmail; // on top of class

public function sendemail()
{       
    $data = array('message' => 'This is a SendgridEmail test!');

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

and when I run the code I found the error message as below

(2/2) ErrorException htmlspecialchars() expects parameter 1 to be string, object given (View: C:\xampp\htdocs\bowoot\resources\views\emails\templateUserRegister.blade.php) in helpers.php (line 547)

I am unable to understand what the issue is. Please help.

Upvotes: 4

Views: 1324

Answers (1)

Oukaasha Habib
Oukaasha Habib

Reputation: 89

If the information provided is accurate, you are returning a view emails.templateUserRegister and it should be email.templateUserRegister. (Notice the s) The reason I'm saying this is because this is your view path.

views/email/templateUserRegister.blade.php

And it definitely doesn't have an 's'.

Edit

Instead of doing this:

return $this->view('emails.templateUserRegister')
                ->from($address, $name)                    
                ->subject($subject)
                ->with([ 'message' => $this->data['message'] ]);

Try this:

$message = $this->data['message'];
return $this->view('emails.templateUserRegister')
                ->from($address, $name)                    
                ->subject($subject)
                ->with('message', $message);

And make $data in

app\Mail\SendgridEmail.php

private or protected.

If this doesn't work, try sending $data from controller as a string and not as an array. Remaining code will remain the same and this line would change:

->with([ 'message' => $this->data['message'] ]);

to:

->with('message', $this->data);

And you still need to change access of $data to private or protected.

EDIT 2

If you check Laravel's documentation for mail, it says this:

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 to fix the issue, just change $message to some other name like $data or $text. Change this:

->with([ 'message' => $this->data['message'] ]);

to this:

->with( 'text', $this->data['message'] );

I hope this fixes the issue.

Upvotes: 1

Related Questions