Undefine variable via post method in sending email from array using laravel 4.2

I was trying to get my data from my form and sending it to an email address. The problem is my variable got undefined. I'm using laravel4.2 in my current project.

This is my form screenshot:

enter image description here

This is the error information:-

enter image description here

This in my form code:

<form method="post" action="{{URL::route('store_mail_contact')}}">
                <div class="row">
                    <div class="col-lg-6 col-md-6 col-sm-12">
                        <div class="form-group{{ ($errors->has('name')) ? ' has-error' : ''}}">
                            <label>Name:</label>
                            <input class="form-control" value="{{Input::old('name')}}" type="text" name="name" placeholder="Enter name...">
                        </div>
                    </div>
                    <div class="col-lg-6 col-md-6 col-sm-12">
                        <div class="form-group{{ ($errors->has('email')) ? ' has-error' : ''}}">
                            <label>Email:</label>
                            <input class="form-control" value="{{Input::old('email')}}" type="email" name="email" placeholder="Enter email...">
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-lg-6 col-md-6 col-sm-12">
                        <div class="form-group{{ ($errors->has('postcode')) ? ' has-error' : ''}}">
                            <label>Postcode:</label>
                            <input class="form-control" value="{{Input::old('postcode')}}" type="text" name="postcode" placeholder="Enter postcode...">
                        </div>
                    </div>
                    <div class="col-lg-6 col-md-6 col-sm-12">
                        <div class="form-group{{ ($errors->has('country')) ? ' has-error' : ''}}">
                            <label>Country:</label>
                            <select class="form-control" name="country"> 
                                <option value="" selected="selected">Select Country</option>
                                @foreach( AIA::all() as $aia) 
                                <option value="{{ $aia->country }}">{{ $aia->country }}</option> 
                                @endforeach 
                            </select>
                        </div>
                    </div>
                </div> 
                <div class="row">
                    <div class="col-md-12">
                        <div class="form-group{{ ($errors->has('subject')) ? ' has-error' : ''}}">
                            <label>Subject:</label>
                            <input class="form-control" value="{{Input::old('subject')}}" type="text" name="subject" placeholder="Enter subject...">
                        </div>
                        <div class="form-group{{ ($errors->has('messages')) ? ' has-error' : ''}}">
                            <label>Messages:</label>
                            <textarea class="form-control" name="messages">{{Input::old('message')}}</textarea>
                        </div>
                    </div>
                </div>
                {{Form::token()}}
                <div class="form-group">
                    <button type="submit" value ="submit" name="submit" class="btn btn-primary">SUBMIT</button>
                    <button type="reset" value ="reset" name="reset" class="btn btn-primary">RESET</button>
                </div>
            </form>

This is my post controller:

public function postContact(){
    $validate = Validator::make(Input::all(),array(
        'name'=>'required',
        'email'=>'required',
        'subject'=>'required',
        'country'=>'required',
        'postcode'=>'required'
    ));
    if ($validate->fails()) {
        return Redirect::route('mail_contact')->withErrors($validate)->withInput();
    }else{
        $mailsending ="[email protected]";
        $name = Input::get('name');
        $subject = Input::get('subject');
        $postcode = Input::get('postcode');
        $country = Input::get('country');
        $email = Input::get('email'); 
        $messages= Input::get('messages');
        $data = array(
            'email_sending'=>$mailsending, 
            'name'=>$name,
            'subject'=>$subject, 
            'postcode'=>$postcode,
            'country'=>$country,
            'email'=>$email,
            'messages'=>$messages
        );
        $mail = Mail::send('emails.message', $data, function($message) use ($mailsending,$name)
        {   
            $message->to($mailsending, $name)->subject($subject);
        });
        if($mail->send()){
            return Redirect::route('mail_contact')->with('success','Your account has been created. We have sent you an email to active your account!');
        }else{
            return Redirect::route('mail_contact')
        ->with('fail','Mail sent not success!');
        }
    }
}

This is my routes:

Route::group(array('before'=>'csrf'),function(){

Route::post('contact',array('as'=>'store_mail_contact','uses'=>'HomeController@postContact'));
});

Upvotes: 0

Views: 85

Answers (1)

LeviTheOne
LeviTheOne

Reputation: 105

I think the error is because the $subject is not seen in the current context, I mean you don't passed it to the callback. Try passing it through use(). I have a working example here:

 $GLOBALS['email'] = $email;
 $GLOBALS['subject'] = $subject;         
                Mail::send('folder.myview', array("key" => $value_passed_to_view), function($message) {
                    $message->to($GLOBALS['email'])
                            ->subject($GLOBALS['subject']);
                });
                unset($GLOBALS['email']);
                unset($GLOBALS['subject']);

Find more information here

Upvotes: 1

Related Questions