Laravel 5.7:如何发送带有多个框的联系表

时间:2018-12-09 12:15:51

标签: php laravel forms laravel-5.7 multiple-variable-return

因此,我有一个正在开发的网站,只是一个带有多个自动滚动部分的引导页面。该页面上有一个新闻专栏,观众可以在其中输入电子邮件地址来进行订阅。这种形式的工作很好。但是问题出在下面,下面有一个“姓名,电子邮件,主题和消息输入”的联系人框,观众可以在其中发送联系人消息。

联系表的基本结构代码:

      {{ Form::open([
                              'action'   =>   'MailController@contactForm',
                              'class'    =>   'contactForm',
                              'method'   =>   'POST',
                              'files'    =>    true,
                                  ]) }}

            <div class="form-group">

              {{  Form::text('name', $value  = NULL, $attributes = array(

                              'id'           =>  'name',
                              'placeholder'  =>  'Your Name',
                              'class'        =>  'form-control',
                              'data-rule'    =>  'minlen:4',
                              'data-msg'     =>  'Please enter at least 4 chars',
                                )) }}

              @yield('div')

              {{  Form::email('email_two', $value  = NULL, $attributes = array(

                              'id'           =>  'email_two',
                              'placeholder'  =>  'Your Email',
                              'class'        =>  'form-control',
                              'data-rule'    =>  'email',
                              'data-msg'     =>  'Please enter a valid email',
                                )) }}

              @yield('div')

              {{  Form::text('subject', $value, $attributes = array(

                              'id'           =>  'subject',
                              'placeholder'  =>  'Subject',
                              'class'        =>  'form-control',
                              'data-rule'    =>  'minlen:4',
                              'data-msg'     =>  'Please enter at least 8 chars of subject',
                                )) }}

              @yield('div')

              {{  Form::textarea('message', $value, $attributes = array(

                              'id'           =>  'message',
                              'placeholder'  =>  'Message or File',
                              'class'        =>  'form-control',
                              'rows'         =>   5,
                              'data-rule'    =>  'required',
                              'data-msg'     =>  'Please write something for us',

                                )) }}

{{ Form::close() }}

它输出带有我的CSS和JS的漂亮表格。 控制器代码:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Mail\Mailer;
use Illuminate\Http\UploadedFile;

class MailController extends Controller {

          protected $request;
          protected $mailer;

          //method to send the subscriber email   (this one works fine)

          public function subscribe (Request $request, Mailer $mailer) {

                  // grab POST data varaiables

                  $this->request  =   $request;

                  if ($request->filled('email_one')) {
                        // $request->flash();
                        $email_one          =   $request->email_one; //$request->input('email_one')
                        $mailer->to($email_one)
                                ->send(new  \App\Mail\Newsletter($email_one));
                        return back();
                      }
          }


          //method to send the contact form is giving me headaches

          public function contactForm (Request $request, Mailer $mailer) {

                        $this->request  =   $request;

                        // $request->flash();

                        $email_two      =   $request->email_two;
                        $name           =   $request->name;
                        $subject        =   $request->subject;
                        $message        =   $request->message;

                  $mailer->to('contactform@domain.com')
                         ->send(new   \App\Mail\ContactForm($data));

                  return back();



                }


}

不起作用的Mailable代码:

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class ContactForm extends Mailable {

            use Queueable, SerializesModels;

            // data to be used in view

            public $email_two;
            public $name;
            public $subject;
            public $message;

            public $data;

            /**
             * Create a new message instance.
             *
             * @return void
             */
            public function __construct($data) {

                  $this->email_two     =     $email_two;
                  $this->name          =     $name;
                  $this->subject       =     $subject;
                  $this->message       =     $message;

                  $data                =     array();

                  $data['email']       =      $this->email_two;
                  $data['name']        =      $this->name;
                  $data['subject']     =      $this->subject;
                  $data['message']     =      $this->message;

            }

            /**
             * Build the message.
             *
             * @return $this
             */
            public function build() {

              return $this->markdown('emails.contactform', compact('data'))
                          // ->subject('Message from Contact Box.')
                          ->from('something@something.com');
                          // ->attach('/path/to/file');
            }


}

联系表格标记的代码:

@component('mail::message')
# New Message Received.

You have received one new message. Please find below:

@component('mail::panel')
From:     {{ $email_two }}    <br>
Name:     {{ $name }}         <br>
Subject:  {{ $subject }}      <br>
Message:  {{ $message }}      
@endcomponent

@component('mail::panel')
This is the panel content.
@endcomponent

Thanks,<br>
{{ config('app.name') }}
@endcomponent

朋友,就是这样。当我提交时事通讯框时,它可以成功运行。我正在使用MailTrap进行测试。但是,当我填写联系方式表格时,它什么也没做。页面无法加载。没有消息。 当我尝试仅发送一个变量(例如主题,名称,电子邮件或消息)时,它可以工作。但是发送多个变量不起作用。

请咨询。

1 个答案:

答案 0 :(得分:0)

这里有很多问题。看来您不太了解类中PHP变量的可见性,因此请阅读documentation。例如,行$this->request = $request是不必要的,什么也不做。

接下来,当您执行send(new \App\Mail\ContactForm($data))时,尚未定义$data。您可以执行$data = $request->all();或将上面的变量设置为$data['email_two'] = $request->input('email_two');等。

然后,在您的Mailable中,您可以将整个构造函数代码替换为$this->data = $data;

最后,在构建函数中,将行$data = $this->data;添加为第一行。