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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法