3
votes

I'm trying to build contact form and I want it to send the user message for the website email and i want it to send message for the user mail inform him that his message received so I'm using this code in controller :

 public function mail(Request $request) {
     Mail::send('mail.mail', ['name'=>"$request->name" , 'email'=>"$request->email" , 'msg'=>"$request->message"], function($message) {
         $message->to('[email protected]', 'Housma')->subject('Housma.com enquiry');
     });

     Mail::send('mail.mailResponse', ['name'=>"$request->name"  ], function($message ) {
        /*line 29 */    
        $message->to("$request->email", "$request->name")->subject('Housma.com : Auto reply');
     });

     return Redirect::to('/contact')->with('successful', 'Your message has been sent');
}

The first message for my email is working fine, but when Laravel reaches the second message, I get this error

ErrorException in pagesController.php line 29: Undefined variable: request

3
its not working , its giving the same error - E-housma Mardini

3 Answers

3
votes

It's not that you can't use it twice, but that the Mail::send can't access it. You need to pass it in with the use statement:

 Mail::send('mail.mailResponse', ['name'=>"$request->name"  ], function($message ) use ($request) {
1
votes

Replace line 28 with

Mail::send('mail.mailResponse', ['name'=>"$request->name"  ],
 function($message) use($request) {

In PHP, if you want to use a variable in a closure, you need to use use ($variablename)

0
votes

May be you should pass $request to the closure. like this !

Mail::send('mail.mailResponse', ['name'=>"$request->name"  ], function($message ) use ($request) {

/*line 29 */    $message->to("$request->email", "$request->name")->subject('Housma.com : Auto reply');
    });

      return Redirect::to('/contact')->with('successful', 'Your message has been sent');
    }