无法使用带有Laravel 5.4的sendgrid发送电子邮件,并发现错误

无法使用带有Laravel 5.4的sendgrid发送电子邮件,并发现错误,laravel,sendgrid,Laravel,Sendgrid,我是sendgrid的新手,希望将sendgrid与Laravel集成。在这里我试着 -在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

我是sendgrid的新手,希望将sendgrid与Laravel集成。在这里我试着 -在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 = 'demotest@gmail.com';
    $subject = 'This is a demo!';
    $name = 'Sam';

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

}
}
-创建模板文件views/email/templateUserRegister.blade.php作为

<!DOCTYPE html>
<html lang="en-US">
<head>
    <meta charset="utf-8">
</head>
<body>
    <h2>Bowoot Email</h2>
    <p>{{ $message }}</p>
</body>
</html>
当我运行代码时,我发现错误消息如下

(2/2)错误异常 htmlspecialchars()要求参数1为字符串,对象为给定对象(视图:C:\xampp\htdocs\bowoot\resources\views\email\templateUserRegister.blade.php) 在helpers.php中(第547行)


我无法理解问题是什么。请提供帮助。

如果提供的信息准确,则返回的是查看
电子邮件。templateUserRegister
,应该是
电子邮件。templateUserRegister
。(请注意s) 我之所以这么说是因为这是你的视角路径

views/email/templateUserRegister.blade.php

而且它肯定没有“s”

编辑

而不是这样做:

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

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

app\Mail\SendgridEmail.php

private
protected

如果这不起作用,请尝试以字符串而不是数组的形式从控制器发送
$data
。其余代码将保持不变,此行将更改:

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

您仍然需要将
$data
的访问权限更改为
私有
受保护

编辑2

如果您查看Laravel的文档,它会说:

注意:$message变量始终传递给电子邮件视图,并允许 附件的内联嵌入。因此,最好避免通过考试 视图有效负载中的消息变量

因此,要解决此问题,只需将
$message
更改为其他名称,如
$data
$text
。更改此项:

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

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

我希望这能解决问题。

对不起!在我的代码中,路径是views/emails/templateUserRegister.blade.php。以上已更正我按照您的建议做了,但仍然收到相同的错误消息htmlspecialchars()期望参数1为字符串,给定对象(视图:C:\xampp\htdocs\mysite\resources\views\emails\templateUserRegister.blade.php)它在本地网站上使用相同的用户名和密码。它抛出一个错误,因为=====================================预期响应代码为250,但收到代码“535”,消息为“535-5.7.8用户名和密码未被接受。有关详细信息,请访问535.7.8 c69-v6sm9501842pfg.2-GSMTP您使用的是哪种托管服务?是否为000webhostapp?
->with([ 'message' => $this->data['message'] ]);
->with( 'text', $this->data['message'] );