Laravel 5.3将数据从控制器传递到邮件视图

时间:2016-10-05 10:18:11

标签: php laravel email

如何将我从邮件控制器获取的数据传递到邮件视图? 在邮件本身,它只是空的

这是我的邮件控制器

 public function send(Request $request)
    {
    //$input = $request->all();
    $name = $request->name;
    $sender_mail = $request->sender_mail;
    $subject = $request->subject;
    $content = $request->content;
    Mail::to($sender_mail)->send(new TestMail($name, $sender_mail, $subject, $content));
    return redirect('/');
    }

这是我的邮件类

    namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class TestMail extends Mailable
{
    use Queueable, SerializesModels;

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

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

这是邮件模板,我最终希望日期结束

<h2>Name: {{ $name }}</h2>
<h2>Sender: {{ $sender_mail }}</h2>
<h2>Subject: {{ $subject }}</h2>
<p>Content: {{ $content }}</p>

2 个答案:

答案 0 :(得分:0)

试试这个:

$data = array('emailId' => $mailId, 'mailBody' => $mailBody);
Mail::send('mail.'.$mailTemplate, // emailVerifyTemplate is the name of template
['data' => $data], 
function ($message) use ($data) {
    $message->from('help@infraprix.com', 'Infraprix');
    $message->to($data['emailId'])->subject('Email Verification - Infraprix');
});

<!-- common email template used by notification class -->
<div>
    <div><?php echo $data['mailBody']; ?></div><br>
    <div>Thanks,</div>
    <div>Team Infraprix</div>
</div>
<!-- common email template used by notification class -->

答案 1 :(得分:0)

如果您在对象公共字段中包含此数据,则传递此对象,如下所示:

public function build()
{
    return $this->view('mail.test', ['data' => $this]);
}

然后在刀片部分:

<h2>Name: {{ $data->name }}</h2>
<h2>Sender: {{ $data->sender_mail }}</h2>
<h2>Subject: {{ $data->subject }}</h2>
<p>Content: {{ $data->content }}</p>