Avi
Avi

Reputation: 798

variables within the Mail function in Laravel 5

I'm trying to use a variable within the Mail function in Laravel 5 and it seems to be erased somehow. Here is what I have:

public function SendEmail($data){


         Mail::send('emails.issue-found', $data, function ($message) {
         // dd($data);
        $message->from('[email protected]', 'my name');
        $message->subject('Alert!');
        $message->to($data['name']['email']);
    });
}

Maybe it sends the actual $data? In the row $message->to($data['name']['email']); I get an error:

Undefined variable: data

I tried to put $data in a different variable but that didn't work either. Any help would be mush appreciated.

Upvotes: 1

Views: 341

Answers (2)

haakym
haakym

Reputation: 12358

Passing the $data variable as a parameter of the Mail::send function only makes it accessible to the email view. You have to utilise use to access data within the anonymous function:

public function SendEmail($data){
    Mail::send('emails.issue-found', $data, function ($message) use ($data) {
        $message->from('[email protected]', 'my name');
        $message->subject('Alert!');
        $message->to($data['name']['email']);
    });
}

You can read more about anonymous functions here: http://php.net/manual/en/functions.anonymous.php

Upvotes: 3

Avi
Avi

Reputation: 798

Like @haakym wrote, except the use $data needs to be use ($data).

Like so -

public function SendEmail($data){
Mail::send('emails.issue-found', $data, function ($message) use ($data) {
    $message->from('[email protected]', 'my name');
    $message->subject('Alert!');
    $message->to($data['name']['email']);
});

}

Thanks a lot for your help!

Upvotes: 2

Related Questions