Christianus Andre Christianus Andre -4 years ago 176
PHP Question

Mailgun sending email laravel

i use mailgun and the setting is done and i've test it and work, but i dont understand why i can't send email without array, here i tried using array but idk why it's error said Undefined variable: data

public function kirim(Request $request){

$data = array(
'email_address'=>$request->email_address,
'cc'=>$request->cc,
'subject'=>$request->subject,
'keterangantambahan'=>$request->keterangantambahan
);

Mail::send('laporan.kirim', $data, function($message) {
$message->from('christian7andrew@gmail.com', 'PuraBox');
$message->to($data['email_address']);
});

return redirect('/');
}


any idea how to use array corectly ??

Answer Source

Use a use.

Looks like you are using a php version which supports closures

Mail::send('laporan.kirim', $data, function($message) use ($data) {
    $message->from('christian7andrew@gmail.com', 'PuraBox');
    $message->to($data['email_address']);
});

The second parameter of the send() method is to set mail options. Does not place the variable inside the function body.

The use puts variables into the body of the function

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download