duanlu4371 2017-08-13 06:29
浏览 48
已采纳

无法通过Laravel 5联系表发送电子邮件

I'm new to Laravel and trying to send emails via contact form.

This is the html form:

<form onsubmit="return false" method="post" id="contact_form" name="contact-form">          
   <input type="hidden" name="_token" value="{{ csrf_token() }}">
   <input type="text" placeholder="Full Name" name="fullname" id="fullname" required>
   <input placeholder="Email Address" required name="email" id="email">
   <button id="contact_btn"data-loading-text="<i class='fa fa-circle-o-notch fa-spin'></i> Sending" >Send Your Message</button>
</form>

This is the controller:

public function contactPost(Request $request) {
    $this->validate($request, [
        'email'=>'required|email',
        'fullname'=>'max:50',
        ]);
    $data = array(
        'email' => $request->email,
        'fullname' => $request->fullname,

        );
    Mail::send('emails.contact', $data, function($message) use ($data){
        $message->from($data['email']);
        $message->to('**@gmail.com');
    });
    return ['success' => true,];
}

This is the route:

Route::post('contact', 'PagesController@contactPost');

This is the last error I could find on laravel.logs:

[2017-08-11 06:30:53] local.ERROR: ErrorException: Use of undefined constant csrf_token - assumed 'csrf_token' in C:\xampp\htdocs\paygizer\storage\framework\views\b9aff2e30c0efeff3b03704fbc6a957f360869cf.php:385

No matter what I do the error doesn't update since yesterday, I restarted the server several times.

This is the ajax call:

$('#contact_form').submit(function(e) {
  e.preventDefault();
}).validate({ 
  rules: {
    fullname: {
      required: true,

    },
    email: {
      required: true,
      email: true
    },

  },
  submitHandler: function (form) {
    var btn = $('#contact_btn');
    btn.button('loading');
    setTimeout(function() {
      btn.button('reset');
    }, 8000);
    $.ajaxSetup({
        headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
      type: 'POST',
      url: "{{url('contact')}}",
  // data: form.serialize(),
  dataType: 'json',
  async: true,
  data: {
    fullname: $('#fullname').val(),
    email: $('#email').val(),
  },
  success: function (json) {
    $('#contact').hide();
    $('#output').html(
      '<i class="fa fa-check" aria-hidden="true" id="check" style="border-radius:50%;font-size:80px;text-align:center;color:#E81B00"></i><br><p class="lead" style="font-size:40px;">We have received your message!</p>'
      ).show();
  },
   error: function(data){
        alert(data);
    },
});
   return false; // for demo
   } 

});

No errors on Chrome dev tools. When I hit submit the button loads and nothing happens.

  • 写回答

1条回答 默认 最新

  • ds19891231 2017-08-13 06:38
    关注

    1) try replacing

    <input type="hidden" name="_token" value="{{ csrf_token() }}">
    

    with

    {{ csrf_field() }}
    

    2) since it is an ajax call are you sure csrf-token meta exists?

    <meta name="csrf-token" content="{{ csrf_token() }}">
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b