Use php variables in Laravel to send mail

I meet, I have this code in my controller:

$name       =   Input::get('fname')." " .Input::get('lname');
$message    =   Input::get('message');
$email      =   Input::get('email');
$subject    =   Input::get('subject');
$phone      =   Input::get('phone');
$area       =   \App\Area::find(1)->name;
$ticket =   \App\Area::find(1)->ticket_sent;
Mail::send('help.send', [ 'Mmessage' => $message, 'Mname' => $name, 'Memail' => $email, 'Msubject' => $subject, 'Mphone' =>$phone, 'Marea' => $area, 'Mticket' => $ticket], function ($message)
    {
        $message->from('asd@gmail.com', 'Name');
        $message->to('support@mycompany.it');
        $message->subject('Support');

    });
$macroArea = MacroArea::find(1);
$macroArea->ticket_sent =$ticket+1;
$macroArea->save();
Session::flash('message', 'The message was successfully send. We will get back to you within 2 business days!');
return Redirect::to('helpDesk');

It works so well, but I'm trying to change two lines in:

$message->from($email, $name);
$message->subject($subject);

But that will not work. What am I doing wrong? I do not understand.

+4
source share
2 answers

please check the code, add use after "function ($ message)"

$name      =   Input::get('fname')." " .Input::get('lname');
$message   =   Input::get('message');
$email     =   Input::get('email');
$subject   =   Input::get('subject');
$phone     =   Input::get('phone');
$area      =   \App\Area::find(1)->name;
$ticket    =   \App\Area::find(1)->ticket_sent;

Mail::send('help.send', [ 'Mmessage' => $message, 'Mname' => $name, 'Memail' => $email, 'Msubject' => $subject, 'Mphone' =>$phone, 'Marea' => $area, 'Mticket' => $ticket], function ($message) use ($email, $name,$subject)
    {
        $message->from($email, $name);
        $message->to('support@mycompany.it');
        $message->subject($subject);
    });
$macroArea = MacroArea::find(1);
$macroArea->ticket_sent =$ticket+1;
$macroArea->save();
Session::flash('message', 'The message was successfully send. We will get back to you within 2 business days!');
return Redirect::to('helpDesk');
+4
source

You need to use use()with anonymous functions to pass variables:

.... function ($message) use ($email, $name) { ....
+4
source

Source: https://habr.com/ru/post/1653124/


All Articles