Php htmlspecialchars()在尝试使用Laravel发送电子邮件时,希望参数1为字符串
我是拉拉维尔的初学者。我为联系人表单创建函数 我有以下代码:Php htmlspecialchars()在尝试使用Laravel发送电子邮件时,希望参数1为字符串,php,laravel,laravel-5,Php,Laravel,Laravel 5,我是拉拉维尔的初学者。我为联系人表单创建函数 我有以下代码: public function sendContactForm($request) { $this->validate($request, [ 'name' => 'required|string', 'topic' => 'required|string', 'email' => 'required|email',
public function sendContactForm($request)
{
$this->validate($request, [
'name' => 'required|string',
'topic' => 'required|string',
'email' => 'required|email',
'message' => 'required|string',
'captcha' => 'required|captcha',
'acceptReg' => 'required|integer',
]);
$adminEmail = $this->frontendRepository->getSystemAdminEmail();
$title = $request->input('topic');
$txt = nl2br($request->input('message'));
$userName = $request->input('name');
$email = $request->input('email');
$ip = $request->getClientIp();
$dateTime = date('Y-m-d H:i:s');
$mailTitle = "Masz wiadomość ze strony". env('APP_NAME')."<br/>";
$message = "
<b>Dane wiadomości:</b> $dateTime [$ip]<br/>
<b>Tytuł wiadomości:</b> $title<br/>
<b>Imię:</b> $userName<br/>
<b>Adres email:</b> $email<br/>
<b>Wiadomość:</b> $txt<br/>";
Mail::to($adminEmail)->send(new ContactMail($message, $mailTitle, $email, $adminEmail));
die('mail sent!');
}
我的联系人form.blade.php:
@section('content')
{{ $message }}
@endsection
运行脚本时出现错误:
htmlspecialchars希望参数1是字符串,对象给定视图:>/var/www/project/resources/views/mail/contactform.blade.php
如何修复它?配置邮件对象并返回self需要您的Mailable类的build方法。所以删除->发送。。呼叫链末端的呼叫:
public function build()
{
return $this->subject($this->title)
->from($this->sender)
->to($this->adminMail)
->replyTo($this->sender)
->view('mail.contactform');
}
编辑:
htmlspecialchars希望参数1是字符串,对象给定视图:>/var/www/project/resources/views/mail/contactform.blade.php
问题存在于邮件模板文件contactform.blade.php中-您不能在邮件类中使用变量名$message,因为Laravel本身使用它将邮件对象传递给模板,所以在模板中$message包含对对象的引用,而不是消息字符串。因此,您可以认为它是由框架保留的
要修复此问题,请在ContactMail类和contactform.blade.php中将$message字段重命名为其他内容,例如,$content或$text、$body等。Wait。。你完全改变了问题?不要那样做!如果第一个问题已解决,请通过自己发布答案来结束此问题,或将其删除。然后用新问题写一个新问题。它不起作用:我有电话:Mail::to$adminEmail->sendnew ContactMail$message,$mailTitle,$email,$adminEmail;和构建:公共函数构建{return$this->subject$this->title->from$this->sender->to$this->adminMail->replyTo$this->sender->view'mail.contactform',['message'=>$this->message];}和模板:@section'content'{{$message@endsection-当我运行脚本时,出现错误:htmlspecialchars期望参数1为字符串,对象给定视图:/var/www/project/resources/views/mail/contactform.blade。php@trafficker我已经更新了我的答案,请检查新的编辑部分
public function build()
{
return $this->subject($this->title)
->from($this->sender)
->to($this->adminMail)
->replyTo($this->sender)
->view('mail.contactform');
}