Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Man's avatar
Level 2

Queue is stripping out data

I have a contact form that sends me an email after someone fills it out. Everything worked great, until i put the contact form in a queue. Now the email comes to me without the user's data, This is the controller:

        public function getContactUsForm(){

            //Get all the data and store it inside Store Variable
            $data = Input::all();

            //Validation rules
            $rules = array (
            'name' => 'required',
            'email' => 'required|email',
            'subject' => 'required',
            'msg' => 'required',
            );

            //Validate data
            $validator  = Validator::make ($data, $rules);

            //If everything is correct then run passes.
            if ($validator -> passes()){

                //Send email using Laravel send function
                Mail::Queue('enquiry', $data, function($msg) use ($data)
                {
                    
            $msg->to('my@email.com', 'Enquiry')->subject('contact request');

                });

                return Redirect::to('/')->with(array('note' => 'Thank you! Your message has been recieved.', 'note_type' => 'success') );  
            }else{
//return contact form with errors
                return Redirect::to('/contact')->withErrors($validator);
            }
        }
}

This is the enquire.blade.php file:

<!--This is a blade template that goes in email message to site administrator-->
<?php
//get the first name
$name = Input::get ('name');
$email = Input::get ('email');
$subject = Input::get ('subject');
$msg = Input::get ('msg');
$date_time = date("F j, Y, g:i a");
$userIpAddress = Request::getClientIp();
?> 

<h1>We been contacted by.... </h1>

<p>
Name: <?php echo ($name); ?> <br>
Email address: <?php echo ($email);?> <br>
Subject: <?php echo ($subject); ?><br>
Message: <?php echo ($msg);?><br>
Date: <?php echo($date_time);?><br>
User IP address: <?php echo($userIpAddress);?><br>

</p>

This the email that comes to my inbox:

We been contacted by....

Name:
Email address:
Subject:
Message:
Date: September 8, 2015, 2:59 am
User IP address: 127.0.0.1

Is there something wrong with the code that the queue removes the $data?

0 likes
56 replies
jekinney's avatar

Once you send the mail to the queue you don't have access to the session variables: Ie input::get. You passed in the $data array? You have to reference that variable $data['name'] Etc

1 like
Man's avatar
Level 2

@jekinney sorry I have a noob question. Do I put $data['name'] in the enquire.blade.php instead of Input::get ('name')? I am a little lost.

jekinney's avatar

You can in line it.

If it's a blade template you don't need the <?PHP or set the variables. I suggest setting all your data and pass it in like you did except the date and user ip. Add those in your controller to your data array that's already being passed in to your mail code. I'm on my mobile so no ticks to show a code block.

Use the blade syntax curly braces and the $data['array key'] no need to set a different variable.

In your view (email template) use the variable to out put the data from the array

1 like
Man's avatar
Level 2

@jekinney Would it be something like this?

<?php

Input::get($data['name', 'email', 'subject', 'msg']);
?> 

<h1>We been contacted by.... </h1>

<p>
Name: $data('name'); <br>
Email address: $data('email');<br>
Subject: $data('subject'); <br>
Message: $data('msg');<br>
</p>
jekinney's avatar

No. You need brackets not parenthesis.

Second drop the input in your view.

Is your contact view blade.php?

In your controller method getContactUs

$data = $request->all(); $data[] = [$request->getClientIp, 'date'=>date()];

This should give you all the variables you need to pass into your mail.

1 like
jekinney's avatar

If someone doesn't beat me to it in my morning I'll show you with a code block.

Man's avatar
Level 2

@jekinney please bare with me, is this closer to what you mean? Controller:

        public function getContactUsForm(){

            //Get all the data and store it inside Store Variable
            $data = $request->all(); 
        $data[] = [$request->getClientIp, 'date'=>date()];

            //Validation rules
            $rules = array (
            'name' => 'required',
            'email' => 'required|email',
            'subject' => 'required',
            'msg' => 'required',
            );

            //Validate data
            $validator  = Validator::make ($data, $rules);

            //If everything is correct then run passes.
            if ($validator -> passes()){

                //Send email using Laravel send function
                Mail::Queue('enquiry', $data, function($msg) use ($data)
                {
            //email 'From' field: Get users email add and name
                 
            //email 'To' field: cahnge this to emails that you want to be notified.                    
            $msg->to('support@thehautie.com', 'Enquiry')->subject('contact request');

                });

                return Redirect::to('/')->with(array('note' => 'Thank you! Your message has been recieved.', 'note_type' => 'success') );  
            }else{
//return contact form with errors
                return Redirect::to('/contact')->withErrors($validator);
            }
        }
}

email view:

<?php
$data['name', 'email', 'subject', 'msg'];

?> 

<h1>We been contacted by.... </h1>

<p>
Name: $data['name']; <br>
Email address: $data['email'];<br>
Subject: $data['subject']; <br>
Message: $data['msg'];<br>
</p>

</p>

The contact page is a blade view.

Man's avatar
Level 2

the $request variable in the controller is throwing an undefined error

karlos545's avatar

Okay if you do

$data = $request->input();

then use that as the data object you pass through you'll be able to access Input::get('name') by literally typing the variable name

<p>
Name:$name <br>
Email address: $email<br>
Subject:$subject <br>
Message: $msg<br>
</p>

with blade syntax around the variables of course

Or you could be more explicit and write:

$data = [
            'name' => $request->input('name'),
            'phone' => $request->input('phone'),
            'email' => $request->input('email'),
            'message_form' => $request->input('msg'),
        ];

@Man

1 like
Man's avatar
Level 2

@karlos545 the $request still throws an undefined variable error, and is $data = $request->input(); for the controller or the email being sent?

karlos545's avatar

You need to pass in the Request object

 public function getContactUsForm(Request $request){

// Don't forget to import it at the top

Man's avatar
Level 2

@karlos545 now i get this error:

Call to undefined method Illuminate\Support\Facades\Request::input()
karlos545's avatar

its not a Facade

use

$data = $request->input();

// Not 

$data = $request::input();

@Man :)

Man's avatar
Level 2

Yes, that is what i am using that is throwing the error

thomaskim's avatar

This is just a guess since we can't see your code, but I'd be willing to bet that it's throwing that error because you imported the Request facade at the top since you probably use the Request facade elsewhere in the controller.

PLB-RR's avatar

Put this in your controller.

$data['name'] = Input::get ('name');
$data['email'] = Input::get ('email');
$data['subject'] = Input::get ('subject');
$data['msg'] = Input::get ('msg');
$data['date_time'] = date("F j, Y, g:i a");
$data['userIpAddress'] = Request::getClientIp();
Man's avatar
Level 2

This is the entire controller as it stands:

<?php 

use \Input as Input;
use \Session as Session;
use \Redirect as Redirect;
use \Validator as Validator;

class ContactController extends Controller {



     //Server Contact view:: we will create view in next step
        public function getContact(){

            return View::make('contact');
        }

        //Contact Form
        public function getContactUsForm(Request $request){

            //Get all the data and store it inside Store Variable
            $data = $request->all(); 
            $data[] = [$request->getClientIp, 'date'=>date()];
            

            //Validation rules
            $rules = array (
            'name' => 'required',
            'email' => 'required|email',
            'subject' => 'required',
            'msg' => 'required',
            );

            //Validate data
            $validator  = Validator::make ($data, $rules);

            //If everything is correct then run passes.
            if ($validator -> passes()){

                //Send email using Laravel send function
                Mail::Queue('enquiry', $data, function($msg) use ($data)
                {
            //email 'From' field: Get users email add and name
                 
            //email 'To' field: cahnge this to emails that you want to be notified.                    
            $msg->to('my@email.com', 'Enquiry')->subject('contact request');

                });

                return Redirect::to('/')->with(array('note' => 'Thank you! Your message has been recieved.', 'note_type' => 'success') );  
            }else{
//return contact form with errors
                return Redirect::to('/contact')->withErrors($validator);
            }
        }
}

I don't see where else the request was coming from.

dbrmr's avatar

Could you show us the email view - 'enquiry' also.

Another thing - how is this working? The command should be:

Mail::queue() // lower case q
PLB-RR's avatar

This is becoming one giant mess ^^

Add Request to your use block.

Man's avatar
Level 2

@dbrmr this is the current enquire.blade.php file. We have been fine tuning it through this forum:

<?php
$data = $request->input();

?> 

<h1>We been contacted by.... </h1>

<p>
Name:$name <br>
Email address: $email<br>
Subject:$subject <br>
Message: $msg<br>
</p>

The Queue alias is upper case in my app.php

Man's avatar
Level 2

@PLB-RR yes it is becoming a bit of a mess. adding request to the use did not solve the error.

Man's avatar
Level 2

The initial code in the question works, I just want it to work when it is passed through the queue.

jekinney's avatar

@Man

<?php 

use Illuminate\Http\Request; // preferred Request Facade instead of Input;
use Carbon\Carbon; // Set the date in a carbon instance for flexibility.
use \Session as Session;
use \Redirect as Redirect;
use \Validator as Validator;

class ContactController extends Controller {



     //Server Contact view:: we will create view in next step
        public function getContact(){

            return View::make('contact');
        }

        //Contact Form
        public function getContactUsForm(Request $request){         
    
        $data = [
            'name' => $request->get'(name'), 
            'email' => $request->get('emai'l),
            'subject' => $request->get('subject'),
            'msg' => $request->get('msg'),
            'date' => Carbon::now(), 
            'client_ip' => $request->getClientIp(),
        ];
            //Validation rules Validate First less work if an error
            $rules = array (
            'name' => 'required',
            'email' => 'required|email',
            'subject' => 'required',
            'msg' => 'required',
            );

            //Validate data
            $validator  = Validator::make ($data, $rules);

            //If everything is correct then run passes.
            if ($validator -> passes()){
                //Send email using Laravel send function
                Mail::queue('enquiry', $data, function($msg) use ($data)
                {
            //email 'From' field: Get users email add and name
                 
            //email 'To' field: cahnge this to emails that you want to be notified.                    
            $msg->to('my@email.com', 'Enquiry')->subject('contact request');

                });

                return Redirect::to('/')->with(array('note' => 'Thank you! Your message has been recieved.', 'note_type' => 'success') );  
            } // Don't need else statements as a return stops the script from continuing.
            //return contact form with errors
            return Redirect::to('/contact')->withErrors($validator);
        }
}

View: Assuming once again it is a blade view just like any other Laravel view (file name has .blade.php)

<h1>We been contacted by.... </h1>

<p>
Name: {{ $data{'name'] }}<br>
Email address: {{ $data['email'] }} <br>
Subject: {{ $data['subject'] }}<br>
Message: {{ $data['msg'] }}<br>
Date: {{ $data['date'] }}<br>
User IP address: {{ $data['client_ip'] }}<br>

</p>

Setting the date in the view will set when the email was sent not when the email was fired to send (Before queued and after queued recursively). The client IP through the requst as in your original post I am guessing is the email servers IP? Not sure but I don't think it was the user's IP. Setting it in the array at the time of request (Before queuing) you can be assured it is the proper IP.

Test it and let me know!

Man's avatar
Level 2

@jekinney I get an Undefined variable: data error. I edited the data string to :

$data = ['name' => $request->get('name'), 'email' => $request->get('email'), 'date' => date(), 'client_ip' => $request->getClientIp(),];

because i think your original had some minor formatting errors, but I still got the undefined variable: data error

jekinney's avatar

@Man

Yeah, your quick. I edited the above post twice right after each other!!!!! I forgot a ' after email on the first pass.

Where do you get the error?

karlos545's avatar

Dont reference the $data array, the keys within the array get converted to variables so...

$data['name']
// can actually be referenced by entering 
$name

@Man

Man's avatar
Level 2

@jekinney the code gets sent to the queue now, only issue i have is when i run queue:work. I get the error:

 [ErrorException]      
  Undefined index: job
Next

Please or to participate in this conversation.