邮件显示空白的POST状态

时间:2018-01-23 10:01:30

标签: php ajax laravel email vue.js

我在Laravel中有一个联系表格,可以发送2封电子邮件 - 向用户发送确认电子邮件,然后发送给发件人。

我没有收到任何错误,并且在我的网络选项卡中显示POST状态但没有状态代码。单击“发送”时页面会刷新。如果某人可能只是看看并提出建议,那么我可能会遇到错误,无法继续工作。

这是我的HTML:

      <div id="contact">
        <form class="form-horizontal" id="contactForm" role="form">
              {{ csrf_field() }}

              @if(Auth::guest())

              <div class="form-group">
                <label for="exampleInputName2">Name</label>
                <input type="text" class="form-control" id="name" placeholder="Jane Doe" name="name">
              </div>
              <div class="form-group">
                <label for="exampleInputEmail2">Email</label>
                <input type="email" class="form-control" id="email" placeholder="jane.doe@example.com" name="email">
              </div>

              @else

              <div class="form-group">
                <label for="exampleInputName2">Name</label>
                <input type="text" class="form-control" id="name" value="{{ Auth::user()->getFullName() }}" name="name">
              </div>
              <div class="form-group">
                <label for="exampleInputEmail2">Email</label>
                <input type="email" class="form-control" id="email" value="{{ Auth::user()->email }}" name="email">
              </div>

              @endif

              <div class="form-group ">
                <label for="exampleInputText">Your Message</label>
               <textarea id="message" name="message" class="form-control" placeholder="Description"></textarea>
              </div>
              <button type="submit" class="btn btn-default" v-on:click="submitMessage">Send Message</button>
            </form>
      </div>

我的Ajax调用(使用Vue.js):

    var contact = new Vue({
          el: '#contact',
          data: {

          },

          methods: {
            submitMessage: function(){
              $.ajaxSetup({
                headers: {
                  'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr("content")
                }
              });

              $.ajax({
                url: '/pages/contact/sendMessage',
                type: 'POST',
                data: $("#contactForm").serialize(),
                success: function(response){
                  toastr.success(response.response);
                },
                error: function(error){
                  toastr.success(error.error);
                }
              });
            }
          }
        });

我的路线:

Route::post('/pages/contact/sendMessage', 'ContactController@sendMessage');

我的控制器:

public function sendMessage(Request $request){
      $this->validate($request, [
        'name' => 'required',
        'email' => 'required',
        'message' => 'required'
      ]);

      $users = User::all();
      $emails = collect($users)->pluck('email')->all();

      if(Auth::user()){
        $user = Auth::user()->getFullName();
        $email = Auth::user()->email;
      }else{
        $user = $request->input('name');
        $email = $request->input('email');
      }

      $message = $request->input('message');

      Mail::to($emails)->send(new Contact($user, $email, $message));

      Mail::to($email)->send(new ContactConfirm($user, $email, $message));

      $response = [
        'response' => 'Email Successfully Sent',
        'error' => 'Something went wrong'
      ];

      return response()->json($response);

  }

我的邮件:

    public $title;
    public $introLines;
    public $outroLines;
    public $actionText;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($user, $email, $message)
    {
      $this->title = 'Contact from' . ' ' . $user;
      $this->$introLines = ['The following user has issued a message'];
      $this->$outroLines = ['User: ' . $user, 'Email: ' . $email, 'Message: ' . $message];
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('emails.template');
    }

我的.env:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=email@gmail.com
MAIL_PASSWORD='app password'
MAIL_ENCRYPTION=tls

我还更新了config/mail.php以获得与上述相同的信息。在.env更改后,我确实停了几次并开始服务。

2 个答案:

答案 0 :(得分:0)

这可能不是问题,但你应该做出这样的改变:

// you are wrapping a collection in collect() and chaining all() incorrectly
$users = User::all();
$emails = collect($users)->pluck('email')->all();

// instead simply do
$emails = User::all()->pluck('email');

这一行虽然是向您数据库中的每个用户发送一封电子邮件。

  Mail::to($emails)->send(new Contact($user, $email, $message))

修改

根据该日志错误,您应该更改此邮件配置

'ssl' => [
   'allow_self_signed' => true,
   'verify_peer' => false,
   'verify_peer_name' => false,
],

答案 1 :(得分:0)

我认为问题实际上是你的提交&#34;按钮。默认情况下,type=submit按钮会提交给表单&#34; action =&#39;&#39;&#34;&#34;你没有的属性,这将导致表单/页面刷新。

将按钮更改为

`<button type="button">` 

然后这将阻止默认操作,然后将触发您的ajax调用。

或者我认为你也可以使用

`<button @click.prevent="submitMessage">` 

基本上会做同样的事情。

如果需要,您还可以将.prevent指令放在表单元素本身上。

<form v-on:submit.prevent></form>