我是第一次尝试使用 Laravel 的 Mail 类,但遇到了一些困难。尝试将联系表单提交到邮件类时,出现 undefined variable 错误。
Controller
public function store()
{
$validation = new Services\Validators\Contact;
if($validation->passes()) {
$fromEmail = Input::get('email');
$fromName = Input::get('name');
$subject = "Email from user at website.com";
$data = Input::get('message');
$toEmail = 'test@dummyemail.com';
$toName = 'Mitch Glenn';
Mail::send('emails.contact', $data, function($message) use ($toEmail, $toName, $fromEmail, $fromName, $subject){
$message->to($toEmail, $toName);
$message->from($fromEmail, $fromName);
$message->subject($subject);
});
return Redirect::to('/')
->with('message', 'Your message was successfully sent!');
}
return Redirect::back()
->withInput()
->withErrors($validation->errors);
}
查看电子邮件.contact
<html>
<body>
Message: {{ $data->message }}
</body>
</html>
我很困惑为什么没有将 $data
变量传递给 View 。我收到此错误:Undefined variable: data
感谢您提供任何帮助或见解。
最佳答案
改变下一行
$data = Input::get('message');
对此
$data = [ 'msg' => Input::get('message') ];
在你的 View
中你可以使用
Message: {{ $msg }}
更新:在将data
传递给View
时,不要使用message
作为变量名。
关于php - 使用邮件类的 Laravel 4 联系表,错误未定义 $data 来自包含电子邮件的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21566007/