Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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('[email protected]', 'PuraBox');
        $message->to($data['email_address']);
    });

    return redirect('/');
}

any idea how to use array corectly ??

like image 361
Christianus Andre Avatar asked Dec 11 '25 04:12

Christianus Andre


1 Answers

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('[email protected]', '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

like image 200
Unamata Sanatarai Avatar answered Dec 12 '25 19:12

Unamata Sanatarai



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!