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 脱敏项目合作,ner需求合作
  • ¥15 脱敏项目合作,ner需求合作
  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密