S M Abrar Jahin
S M Abrar Jahin

Reputation: 14588

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: 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?

Upvotes: 2

Views: 821

Answers (1)

Bogdan
Bogdan

Reputation: 44526

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.

Upvotes: 5

Related Questions