我已经为发送电子邮件编写了这段代码
Route::post('contact', function(){
$inputs = Input::all();
$rules = array(
'email' => 'required|email',
'name' => 'required|min:2',
'message' => 'required',
'recaptcha_response_field' => 'required|recaptcha',
);
$validator = Validator::make($inputs, $rules);
if($验证器->通过()){
$fromEmail = Input::get('email');
$fromName = Input::get('name');
$subject = Input::get('subject');
$data = array('message' => Input::get('message'));
$toEmail = 'info@danielchikaka.com';
$toName = 'Daniel Chikaka';
Mail::send('emails.contact', $data, function($message) use ($toEmail, $toName, $fromEmail, $fromName, $subject){
$message->to($toEmail, $toName)->from($fromEmail, $fromName)->subject($subject);
});
return Redirect::to('/');
}
return Redirect::to('/#contact')->withInput()->withErrors($validator);
});
和我的视图邮件。联系方式
<html>
<body>
<p><b>Email From:</b> {{$fromName}} of {{$fromEmail}}</p>
<p><b>Subject:</b> {{$subject}}</p>
<b> Message:</b> <br>
</html> {{$data}}
</body>
但是每当我发送电子邮件时,我收到的都是:
Email From:{{$fromName}} of {{$fromEmail}}
Subject: {{$subject}}
Message:
{{$data}
为什么我的视图没有从邮件类中获取数据?
确保您的视图具有blade
扩展名:
views/emails/contact.blade.php
$data数组只包含变量message。因此,在您的刀片模板中,只有{{$message}}可以工作。确保将视图中所需的所有变量都传递到$data数组中。
$data = array(
'message' => Input::get('message'),
'fromName' => Input::get('name'),
'fromEmail'=> Input::get('email')
);
现在,在您的contact.blade.php中,您可以将它们用作常规变量{{$formName}}, {{$fromEmail}}等;
另外,由于您正在使用刀片模板,请确保您的视图文件具有适当的扩展名,即contact.blade.php
我找到了解决问题的方法:
$fromEmail = Input::get('email');
$fromName = Input::get('name');
$subject = Input::get('subject');
$data = array('content' => Input::get('message'));
$toEmail = 'info@danielchikaka.com';
$toName = 'Daniel Chikaka';
Mail::send('emails.contact', $data, function($message) use ($toEmail, $toName, $fromEmail, $fromName, $subject){
$message->to($toEmail, $toName)->from($fromEmail, $fromName)->subject($subject);
});
在视图中必须是:
<html>
<body>
{{$content}}
</html>
</body>
重要:永远不要在联系人视图文件中使用{{$message}},根据Taylor Otwell的说法,这是在电子邮件视图文件中保留的
希望有一天这能帮助到别人!