Laravel 4电子邮件:我无法收到电子邮件

时间:2014-06-01 17:55:17

标签: php email laravel-4

我已经写了这封发送电子邮件的代码

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);

如果(用$ validator->通行证()){

$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);

});

和我的观点emails.contact是

<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}

为什么我的视图没有从Mail Class中获取数据?

3 个答案:

答案 0 :(得分:0)

确保您的视图具有blade扩展名:

views/emails/contact.blade.php

答案 1 :(得分:0)

您的$ data数组仅包含变量消息。因此,在您的刀片模板中,只有{{$ message}}才有效。确保将视图中所需的所有变量传递到$ data数组。

$data = array(
  'message' => Input::get('message'),
  'fromName' => Input::get('name'),
  'fromEmail'=> Input::get('email')
);

现在,在您的contact.blade.php中,您可以将它们用作普通变量{{$ formName}},{{$ fromEmail}}等;

此外,由于您正在使用刀片模板,请确保您的视图文件具有正确的扩展名,即contact.blade.php

答案 2 :(得分:0)

我找到了解决问题的方法:

$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>

重要: 根据Taylor Otwell的说法,切勿在联系人视图文件中使用{{$ message}},这是在电子邮件视图文件中保留的

希望有一天能帮助别人!