Laravel Mailables的问题

时间:2016-12-02 17:58:10

标签: laravel email laravel-5.3

我正在尝试使用Laravel 5.3在Laravel中创建一个联系表单,但我在这里遇到了这个令人讨厌的错误:

  

helpers.php第519行中的ErrorException:   htmlspecialchars()期望参数1为字符串,给定对象(查看:/Applications/XAMPP/xamppfiles/htdocs/meps/resources/views/emails/contactemail.blade.php)

以下是我使用的文件:

联系表格

<div class="contact-form">
    <form class="margin-clear" role="form" action="{{ url('/sendmail') }}" method="POST">

    {{ csrf_field() }}
        <div class="form-group has-feedback">
            <label for="name">Name*</label>
            <input type="text" class="form-control" id="name" name="name" placeholder="">
            <i class="fa fa-user form-control-feedback"></i>
        </div>
        <div class="form-group has-feedback">
            <label for="email">Email*</label>
            <input type="email" class="form-control" id="email" name="email" placeholder="">
            <i class="fa fa-envelope form-control-feedback"></i>
        </div>
        <div class="form-group has-feedback">
            <label for="subject">Subject*</label>
            <input type="text" class="form-control" id="subject" name="subject" placeholder="">
            <i class="fa fa-navicon form-control-feedback"></i>
        </div>
        <div class="form-group has-feedback">
            <label for="message">Message*</label>
            <textarea class="form-control" rows="6" id="message" name="message" placeholder=""></textarea>
            <i class="fa fa-pencil form-control-feedback"></i>
        </div>
        <input type="submit" value="Submit" class="btn btn-primary">
    </form>
</div>

控制器功能

public function sendmail(Request $request, Mailer $mail) {

    $mail->to('kaley36_aw@yahoo.com')->send(new ContactEmail($request->name, $request->email, $request->subject, $request->message));

    $request->session()->flash('mail-sent', 'Your email has been sent.');

    return redirect('/contact');

}

Mailable类

class ContactEmail extends Mailable
    {
    use Queueable, SerializesModels;

    public $name;
    public $email;
    public $subject;
    public $message;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($name, $email, $subject, $message)
    {
        $this->name = $name;
        $this->email = $email;
        $this->subject = $subject;
        $this->message = $message;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->from($this->email)->view('emails.contactemail');
    }
}

这是路线

Route::post('sendmail', 'EmailController@sendmail');

2 个答案:

答案 0 :(得分:0)

你可能看错了。错误指向

/views/emails/contactemail.blade.php

但是这个视图有<form>,除非您通过电子邮件向用户发送表单,这看起来更像是您的联系表单视图,而不是您的电子邮件视图。类似的东西:

/views/contact.blade.php(或其他任何形式的表格)

对于错误,您必须有一个{{ $variable or functionCall() }},它不接收字符串,而是一个对象。

答案 1 :(得分:0)

我明白了。问题在于变量$ message,Laravel将它包装在一个名为Message的实际对象中。我所要做的就是将变量名称更改为$ theMessage,它可以找到。