简体   繁体   中英

Laravel pass variables to mail template?

My problem is that I want to send variables from my EmailController to my mail view and then email to the address. I have tried several things but it didn't work. Hopefully you can help me.

My Controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Mail\Email;
use Mail;

class EmailController extends Controller
{
public function sendMail(Request $request)
{

    $option = $request->input('vraag');
    $title = $request->input('onderwerp');
    $body = $request->input('bericht');
    $email = $request->input('email');
    $gender = $request->input('aanhef');
    $name = $request->input('naam');

    $data = array(
        'option' => $option,
        'title' => $title,
        'body' => $body,
        'e-mail' => $email,
        'gender' => $gender,
        'name' => $name
    );

    $receiverAddress = 'test@example.com';

           return view('emails.email')->with('option', 'title', 'body', 'e-mail', 'gender', 'name');



}
}

I want the request variables to my mail view. I am getting the error: Undefined variable: data.

My View:

<!DOCTYPE html>
<html>
<head>
   <title></title>
</head>
<body>
   <h1>Contactformulier</h1>
   <?php
     echo $data['option'];
   ?>
</body>
</html>

The setup is correct, but you should access $option instead of $data['option'] .

Bonus: you can pass variables to emails using View::share('foo', 'bar')

您可以使用此:

 return view('mailview')->with('name', 'email');

you can pass variables to email template as:

        Mail::send('emails.templatename', $data, function ($m) {
            $m->from(env('INFO_EMAIL'));
            $m->to(env('PROJECT_EMAIL'), 'admin')->subject('Mail Subject');
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM