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 咨询一下有关于王者荣耀赢藏战绩
  • ¥100 求购一套带接口实现实习自动签到打卡
  • ¥50 MacOS 使用虚拟机安装k8s
  • ¥500 亚马逊 COOKIE我如何才能实现 登录一个亚马逊账户 下发新 COOKIE ..我使用下发新COOKIE 导入ADS 指纹浏览器登录,我把账户密码 修改过后,原来下发新COOKIE 不会失效的方式
  • ¥20 玩游戏gpu和cpu利用率特别低,玩游戏卡顿
  • ¥25 oracle中的正则匹配
  • ¥15 关于#vscode#的问题:把软件卸载不会再出现蓝屏
  • ¥15 vimplus出现的错误
  • ¥15 usb无线网卡转typec口
  • ¥30 怎么使用AVL fire ESE软件自带的优化模式来优化设计Soot和NOx?