dounieqi6959 2015-11-25 05:44
浏览 124
已采纳

使用laravel将一个组合文本发送到保存在数据库中的电子邮件

I have created a form, upon its submission it goes to email id which is stored in Laravels mail function. But I want to send same email to multiple people whose email-id are stored in database. Here is my code:

Routes:

Route::get('contactform', 'ClientsController@display1');
Route::post('contactform', 'ClientsController@contact');

My controller:

public function display(){
    $data = Input::get('agree');
    $count = count ($data);
    if($count){
        return $this->contact( $data,$count);
    }
    else{
        echo "Select At least one email-id.";
        return view('clientinfo.backtoprev');
    }

}

    public function display1(){

        return view('contactform');
    }

    public function contact($data,$count){
        $input = Input::only('name', 'email', 'msg');

        $validator = Validator::make($input,
            array(
                'name' => 'required',
                'email' => 'required|email',
                'msg' => 'required',
            )
        );

        if ($validator->fails())
        {
            return Redirect::to('contact')->with('errors', $validator->messages());
        } else { 


            Mail::send('contactemail', $input, function($message) use($data)
            {
                $message->from('your@email.address', 'Your Name');

                $message->to( $data);
            });

            return view('clientinfo.display', compact('data','count'));
        }

    }

here is my view "Contactform"

{!! Form::open(array('action' => 'ClientsController@contact','method' => 'post','name'=>'f1' , 'id'=>'form_id'))!!}  
    @if(Session::has('errors'))
        <div class="alert alert-warning">
            @foreach(Session::get('errors')->all() as $error_message)
                <p>{{ $error_message }}</p>
            @endforeach
        </div>
    @endif
       //My fields, name & message
{!! Form::close() !!}

From the "display" method i'm passing two variables $data and $count to contact method, but after filling form and hitting submit error comes as

Missing argument 1 for App\Http\Controllers\ClientsController::contact()

  • 写回答

2条回答 默认 最新

  • duanlei4759 2015-11-30 07:29
    关注

    Here are corrections that worked for my question.

    I used one controller instead of two:

        public function showForm(Request $request )
        {
            //Get Content From The Form
            $name = $request->input('name');
            $email = Input::get('agree');
            $message = $request->input('message');
    
            //Make a Data Array
            $data = array(
                'name' => $name,
                'email' => $email,
                'message' => $message
            );
    
            //Convert the view into a string
            $emailView = View::make('contactemail')->with('data', $data);
            $contents = (string) $emailView;
    
            //Store the content on a file with .blad.php extension in the view/email folder
            $myfile = fopen("../resources/views/emails/email.blade.php", "w") or die("Unable to open file!");
            fwrite($myfile, $contents);
            fclose($myfile);
    
            //Use the create file as view for Mail function and send the email
            Mail::send('emails.email', $data, function($message) use ($data) {
                $message->to( $data['email'], 'Engage')->from('stifan@xyz.com')->subject('A Very Warm Welcome');
            });
    //        return view();
        }
    

    Routes:

    Route::post('contactform', 'ClientsController@showForm');
    Route::get('/', 'ClientsController@profile');
    

    The view contactemail has the data to be sent, and the view email we are sending through mail function. When the user puts data in the form, that data will get saved in email.blade.php because of these lines of code:

    //Convert the view into a string
            $emailView = View::make('contactemail')->with('data', $data);
            $contents = (string) $emailView;
    
            //Store the content on a file with .blad.php extension in the view/email folder
            $myfile = fopen("../resources/views/emails/email.blade.php", "w") or die("Unable to open file!");
            fwrite($myfile, $contents);
            fclose($myfile);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?