0

我试图从我的表格中获取我的数据并将其发送到一个电子邮件地址。问题是我的变量未定义。我laravel4.2在我当前的项目中使用。

这是我的表单截图:

在此处输入图像描述

这是错误信息:-

在此处输入图像描述

这在我的表单代码中:

<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>

这是我的后控制器:

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 ="likdy2015@gmail.com";
        $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!');
        }
    }
}

这是我的路线:

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

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

1 回答 1

1

我认为错误是因为在当前上下文中没有看到 $subject,我的意思是你没有将它传递给回调。尝试通过它use()。我在这里有一个工作示例:

 $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']);

在此处查找更多信息

于 2016-05-20T07:47:06.717 回答