我将在流明中使用 gmail smtp 发送电子邮件,一切正常,但一个变量始终未定义,请让我知道我错在哪里这是我的代码<?phpnamespace 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”
1 回答
慕尼黑的夜晚无繁华
TA贡献1864条经验 获得超6个赞
你失踪了$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);
});
}
- 1 回答
- 0 关注
- 68 浏览
添加回答
举报
0/150
提交
取消