流明中未定义的变量

我将在流明中使用 gmail smtp 发送电子邮件,一切正常,但一个变量始终未定义,请让我知道我错在哪里


这是我的代码


<?php


namespace App\Services;

use Illuminate\Support\Facades\Mail;


class MailService

{


    public static function send($mail_to = '', $title = '', $content = '') {

        Mail::send('mail', ['title' => $title, 'content' => $content], function ($message) {

            $message->from('noreply@gmail.com', 'Test Mail');

            $message->to($mail_to);

        });

    }

}


这是控制器


public function register(Request $request)

{


    $rules = [

        'name'          => 'required',

        'email'         => 'required|email|unique:users',

        'password'      => 'required|min:5',

        'phone'         => 'required|numeric|min:10',

        'business_name' => 'required|unique:users',

        'business_type' => 'required'

    ];


    $this->validate($request, $rules);


    $data = $request->all();

    $hashPassword = Hash::make($data['password']);


    $data['password'] = $hashPassword;

    $data['is_activated'] = 'false';


    $pin    = mt_rand(1000, 9999); 

    $token  = hash("sha256", $pin);


    $data['token']  = $token;

    $data['otp']    = $pin;


    $user = User::create($data);


    if ($user) {

        MailService::send($request->input('email'), 'OTP', $pin);

        return response()->json(['response' => true, 'message' => 'User registered Successfully', 'token' => $token], 201);

    } else {

        return response()->json(['response' => false, 'message' => ' Please check your credentials, Try again'], 400);

    }

}

这是错误


{消息:“未定义变量:mail_to”,异常:“ErrorException”,文件:“D:\xampp\htdocs\api\app\Services\MailService.php”,行:12,跟踪:Array(28)}


异常:“错误异常”


文件:“D:\xampp\htdocs\api\app\Services\MailService.php” 行:12 消息:“未定义变量:mail_to”


潇湘沐
浏览 69回答 1
1回答

慕尼黑的夜晚无繁华

你失踪了$mail_to。你需要在函数中使用它,然后你可以使用它,否则你会得到一个未定义的变量错误,因为你现在得到它。use($mail_to)您的代码如下所示。public static function send($mail_to = '', $title = '', $content = '') {&nbsp; &nbsp; Mail::send('mail', ['title' => $title, 'content' => $content], function ($message) use($mail_to) {&nbsp; &nbsp; &nbsp; &nbsp; $message->from('noreply@gmail.com', 'Test Mail');&nbsp; &nbsp; &nbsp; &nbsp; $message->to($mail_to);&nbsp; &nbsp; });}
打开App,查看更多内容
随时随地看视频慕课网APP