php - Laravel 5.7: How To Send Contact Form With Multiple Boxes

235

So I have this website I am developing, just a single bootstrap page with multiple auto-scrolling section. The page has a newsletter section where a viewer can input their email address to susbscribe. The form for this works just fine. But the problem is down below, there is a contact box for "Name, Email, Subject and Message inputs" where a viewer can send a contact message.

Basic structure Code for contact form:

      {{ Form::open([
                              'action'   =>   '[email protected]',
                              '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() }}

It outputs a nice pretty form with my css and js. Code for the Controller:

<?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('[email protected]')
                         ->send(new   \App\Mail\ContactForm($data));

                  return back();



                }


}

Code for the Mailable which isnt working:

<?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('[email protected]');
                          // ->attach('/path/to/file');
            }


}

Code for the contact form markdown:

@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

So friends, that is it. When I submit the newsletter box, It works succesfully. I am testing with MailTrap. But when I fill the contact box form, It does nothing. Page doesnt load. No message comes. When I try to send only one variable (e.g. subject, name, email or message), it works. But sending multiple variable doesnt work.

Please advice.

320

Answer

Solution:

There are quite a few issues here. It looks like you don't fully understand the visibility of PHP variables in classes, so have a read of the documentation. For example, the lines$this->request = $request are unnecessary and do not do anything.

Next, when you dosend(new \App\Mail\ContactForm($data)), you have not defined$data. You could do$data = $request->all(); or set the variables above to be$data['email_two'] = $request->input('email_two'); etc.

Then, in yourMailable, you can replace your entire constructor code with$this->data = $data;

Finally, in the build function add the line$data = $this->data; as the first line.

People are also looking for solutions to the problem: php - How can I change the label text in jQuery?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.