douzepao0281 2017-03-25 18:46
浏览 45
已采纳

Laravel 5:发送电子邮件时出错

I am trying to send test emails in my Laravel project, and am encountering the following error:

ErrorException in helpers.php line 532:

htmlspecialchars() expects parameter 1 to be string, object given (View: C:\...esources\views\mail-test.blade.php)

I've been toying around with my code, following some guidelines/tutorials online the best I can, but I don't see what I'm doing wrong. Code snippets are as follows:

web.php

Route::post('/send-mail', 'MailController@send')->name('send-mail');

sample-page.blade.php

...
<div style="text-align: center;">
   <form action="{{ route('send-mail') }}" method="post">
        {{ csrf_field() }}
        <input type="email" name="email" placeholder="Email Address">
        <input type="text" name="message" placeholder="Insert Message Here.">
        <button type="submit">Let's send an email!</button>
    </form>
</div>
....

MailController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Mail\Mailer;

use App\Mail\SendMail;

class MailController extends Controller
{
    public function send(Request $request, Mailer $mailer) {
        $mailer
            ->to($request->input('email'))
            ->send(new SendMail($request->input('message')));
        return back();
    }

}

SendMail.php

...
use Queueable, SerializesModels;

public $message;

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

/**
 * Build the message.
 *
 * @return $this
 */
public function build()
{
    return $this->from('test@test.com')
                ->view('mail-test');
}

mail-test.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Email Test</title>
</head>
<body>
    <h1>EMAIL TESTING</h1>
    <p>{{ $message }}</p>
</body>
</html>
  • 写回答

1条回答 默认 最新

  • doulingqiu4349 2017-03-26 01:04
    关注

    The $message variable is automatically passed into the view by Laravel, and it's an instance of the Illuminate/Mail/Message class. If you have a string of content you need to pass to the view, you should do that in the view() call. But you should rename it from $message to avoid conflict. I believe this may do it for you:

    SendMail.php

    return $this->from('test@test.com')
                ->view('mail-test', ['contentMessage' => $this->message]);
    

    mail-test.blade.php

    <body>
        <h1>EMAIL TESTING</h1>
        <p>{{ $contentMessage }}</p>
    </body>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?