Ad

Send Mail Using The Email Address From A Variable

- 1 answer

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: enter image description here

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?

Ad

Answer

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.

Ad
source: stackoverflow.com
Ad