简体   繁体   中英

Send mail using the email address from a variable

I want to send mail to a variable mailing address, what I am doing is:

$this->create($request->all());
//Send confirmation mail
$email = $request['email'];
Mail::send('auth.emails.welcome', ['token' => 'System'], function ($message)
{
    $message
            ->to(
                    $email,
                    'Laravel 5.2 App'
                )
            ->subject('Activate Your Account !');
});

But I am getting this error: 在此处输入图片说明

Detailed code is:

public function register(Request $request)
{
    $validator = $this->validator($request->all());

    if ($validator->fails()) {
        $this->throwValidationException(
            $request, $validator
        );
    }

    //Auth::login($this->create($request->all()));

    $this->create($request->all());
    //Send confirmation mail
    $email = $request['email'];
    Mail::send('auth.emails.welcome', ['token' => 'System'], function ($message)
    {
        $message
                ->to(
                        $email,
                        'Laravel 5.2 App'
                    )
                ->subject('Activate Your Account !');
    });

    return redirect($this->redirectPath());
}

Can anyone please help?

You need to pass the variable to the closure with the use construct, because the closure has a different scope:

$this->create($request->all());

//Send confirmation mail
$email = $request['email'];

Mail::send('auth.emails.welcome', ['token' => 'System'], function ($message) use ($email) {
    $message->to($email,'Laravel 5.2 App')
            ->subject('Activate Your Account !');
});

You can read more about that in the PHP: Anonymous functions documentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM