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

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)