Debabrata Patra
Debabrata Patra

Reputation: 548

Get recipient name on view in laravel mail

I have a set of recipients. I am able to send mail to all of them. But how to get their name on the view. To be specific how to get $user value in my view(emails.test).

Mail::send('emails.test', ['data' => $data], function ($message) use ($data) {

        foreach($data['users'] as $user) {
            $message->to($user->email, $name = $user->firstName . ' ' . $user->lastName);
        }

        $message->subject('test');
    });

Is there any way to access $user value in my view? I can access $data in my view. $data['users'] is an array of users. I need particular/current User's name in the view.

My view(emails.test)

<div>Dear {{$user->firstName}},</div>
How are you?.... 

But user is undefined here.

Thanks in advance.

Debabrata

Upvotes: 2

Views: 2000

Answers (1)

Amir Bar
Amir Bar

Reputation: 3105

from the docs

The send method accepts three arguments. First, the name of a view that contains the e-mail message. Secondly, an array of data you wish to pass to the view. Lastly, a Closure callback which receives a message instance, allowing you to customize the recipients, subject, and other aspects of the mail message

as you can see the second arguments its the data you send to the view

so in your view you can use the $data array just like you did inside the closure:

@foreach($data['users'] as $user) {
            {{$user->username}}
        }

Upvotes: 1

Related Questions