Ad

Send Dynamic Email In Laravel With SwiftMailer

- 1 answer

i want send dynamic email after registration form in laravel. my codes are:

$gift = new Gift();
        $gift->user_id = Auth::id();
        $gift->register_user = $request->input('email');
        $gift->status = 0;
        $gift->amount = 10;
        $gift->code = uniqid('coinex-');
        if ($gift->save()) {
            $data = array('code'=>$gift->code,'friend'=>Auth::user()->name,'email'=> $gift->register_user);
            $email =  $gift->register_user;
            Mail::send('invite', $data, function($message) {
                $message->to("Receiver_Email_address", Auth::user()->name.' Invited you to Join site')->subject
                (Auth::user()->name.' Invited you to Join Coinex Plus');
                $message->from('[email protected]','site');
            });
            toast('Thank you for your cooperation !', 'success');
            return redirect()->back();
        } else {
            toast('something wrong please try again !', 'danger');
            return redirect()->back();
        }

how can i replace Receiver_Email_address with $email ?

Ad

Answer

You can pass the variable to a closure using use keyword.

Below is the sample code to do that:

Mail::send($invite, $data, function ($message) use ($email) {
     $message->to($email, Auth::user()->name.' Invited you to Join site')->subject
            (Auth::user()->name.' Invited you to Join Coinex Plus');
            $message->from('[email protected]','site');
}

Hope this will work for you.

Best.

Ad
source: stackoverflow.com
Ad