Why email verification function not taking the to address properly?

This is my function.

public function email_verify_send_mail()
    {
        $inputs = \Input::all();
        $rules = array(
            'email' => 'required|email'
        );
         $validation = Validator::make($inputs, $rules);

        if ($validation->fails()) {
            $messages = $validation->errors();
            return View::make('email_verify')->with('errors', $messages);
        }
        else{
            $in=$inputs['email'];
            //send mail to the user.......
            Mail::send('email_view', ['key' => 'value'], function($message)
            {
                $message->to($inputs['email'])->subject('Welcome!');
            });
            return "okie";


        }
    }

here when i print the $inputs['email'] it clearly print the email address. but when i add that to the mail::send function,it gives an error that Undefined variable: inputs.Why it happens?thanks in advance

Answers


the variable inputs is not inside the scope of the anonymous function. please use the "use" statement like below:

public function email_verify_send_mail()
{
    $inputs = \Input::all();
    $rules = array(
        'email' => 'required|email'
    );
     $validation = Validator::make($inputs, $rules);

    if ($validation->fails()) {
        $messages = $validation->errors();
        return View::make('email_verify')->with('errors', $messages);
    }
    else{
        $in=$inputs['email'];
        //send mail to the user.......
        Mail::send('email_view', ['key' => 'value'], function($message) use ($inputs)
        {
            $message->to($inputs['email'])->subject('Welcome!');
        });
        return "okie";


    }
}

Try this....

Mail::send('email_view', ['key' => 'value'], function($message)
            {
                $message->to($inputs['email'])->subject('Welcome!');
            });

to

Mail::send('email_view', ['email' => $in], function($message)
            {
                $message->to($email)->subject('Welcome!');
            });

(or)

  Mail::send('email_view', function($message)  use($in)
                {
                    $message->to($in)->subject('Welcome!');
                });

Refer:https://laravel.com/docs/5.0/mail


Need Your Help

debug.log in CakePHP without stacktrace

debugging cakephp cakephp-2.0

I log values of variables into debug.log using: