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

Laravel 5.7: How To Send Contact Form With Multiple Boxes

所以我正在开发这个网站,只有一个 bootstrap 页面,其中包含多个自动滚动部分。该页面有一个时事通讯部分,观众可以在其中输入他们的电子邮件地址进行订阅。这个表格工作得很好。但问题出在下面,有一个 "Name, Email, Subject and Message inputs" 的联系框,观众可以在其中发送联系信息。

联系表基本结构代码:

      {{ 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 进行测试。但是当我填写联系框表格时,它什么也没做。页面未加载。没有消息传来。 当我尝试只发送一个变量(例如主题、姓名、电子邮件或消息)时,它起作用了。但是发送多个变量不起作用。

请指教

这里有很多问题。看来您还没有完全理解 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; 作为第一行。