流明中的未定义变量

时间:2019-11-26 08:39:44

标签: php laravel lumen

我要在流明中使用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) }

     

例外:“ ErrorException”

     

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

1 个答案:

答案 0 :(得分:1)

您缺少$mail_to。您需要在函数中使用它,然后可以使用它,否则当您现在获取它时,将出现未定义的变量错误。

use($mail_to)

您的代码如下所示。

public static function send($mail_to = '', $title = '', $content = '') {
    Mail::send('mail', ['title' => $title, 'content' => $content], function ($message) use($mail_to) {
        $message->from('noreply@gmail.com', 'Test Mail');
        $message->to($mail_to);
    });
}