如何将类中的电子邮件变量传递到laravel中的刀片

时间:2016-12-13 11:39:32

标签: php laravel

我有这个班级来处理电子邮件。

public function mail($emails, $message)
    {

        Mail::queue('emails.notification', ['message' => $message], function($m) use ($emails) {
            $m->to($emails);
            $m->subject('Notification....');

        });
    }

$ massage参数是从这个类中调用的,

private function report(Notification $notification, $resCode)
    {
        if(empty($resCode)){
            $resCode = "no response found";
        }

        $this->reporter->slack($notification->website_url . ':' . '  is down' . ' this is the status code!' . ' @- ' .$resCode,
             $notification->slack_channel);

        $this->reporter->mail($notification->email,$notification->website_url.' is down '. ' this is the status Code: '. $resCode);


    }

现在我想在emails.notification.blade中显示消息,并尝试这种方式

@extends('layout')
@section('header')
@stop
@section('content')
        <h1>The Web site is down!</h1>
               {{ $message }}


@stop
@section('footer')
@stop


  [ErrorException]
  htmlspecialchars() expects parameter 1 to be string, object given

这是我得到的例外。谁有更好的建议?

3 个答案:

答案 0 :(得分:1)

在控制器上:

$data = array('emailId' => $emailId, 'mailBody' => $mailBody);

$sendMailStatus = Mail::send('mail.emailForgotPassword', ['data' => $data], function ($message) use ($data) {
        $message->from('xyz.com', 'abc');
    $message->to($data['emailId'])->subject('Your Subject');
});

在刀片上:

{{ $data['mailBody'] }}

答案 1 :(得分:0)

基于Sending Mail,您可以将数组形式的变量值作为第二个参数传递。

Mail::send('templatepath', 'blade-variable', function($m) use($user){
     $m->from('from@domain.com', 'From Label');
     $m->to('to@domain.com', 'To Label')->subject('Subject');
});

此处刀片变量是第二个参数。您可以用这个替换数组值。

此外,从您放置的错误开始, htmlspecialchars()要求参数1为字符串,对象为。表示$message变量不是字符串。检查$message值并传递字符串,如:['message' => 'This is the message..!']

希望这会清除你的怀疑!

答案 2 :(得分:0)

关注link

$info = array('firstname'=>'xyz','lastname'=>$lastname);
Mail::queue('emails.notification', ['message' => $info], function($m) use ($emails) {
        $m->to($emails);
        $m->subject('Notification....');

    });

查看页面打印

{{ $message }}