Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use php variables in Laravel to send mail

I mates,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('[email protected]', 'Name');
        $message->to('[email protected]');
        $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 good, but I'm trying to change two rows in:

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

But it doesn't work. What am I doing wrong? I don't understand.

like image 933
therock24 Avatar asked Feb 07 '23 06:02

therock24


2 Answers

please check the code, need to 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('[email protected]');
        $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');
like image 102
rahul Avatar answered Feb 19 '23 02:02

rahul


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

.... function ($message) use ($email, $name) { ....
like image 32
Alexey Mezenin Avatar answered Feb 19 '23 02:02

Alexey Mezenin