2016-03-22 44 views
0

我想用變量主題發送電子郵件,這是代碼Sendmail的主題5.1

public function sendmail(Request $request) 
{ 
    $data = [ 
     'subject' => $request->input('subject'), 
     'name' => $request->input('name'), 
     'phone' => $request->input('phone'), 
     'email' => $request->input('email') 
    ]; 
    Mail::send('mail.sendmail' , $data, function($msg){ 
     $msg->from('mygmail.com', 'Avil'); 
     $msg->to('[email protected]', 'Avil')->subject('Welcome to Laravel 5.1'); 
    }); 
    return redirect()->route('contact.index'); 
} 

我想主題不是「歡迎Laravel 5.1」,主題是

public function sendmail(Request $request) 
{ 
    $data = [ 
     'subject' => $request->input('subject'), 
     'name' => $request->input('name'), 
     'phone' => $request->input('phone'), 
     'email' => $request->input('email') 
    ]; 
    Mail::send('mail.sendmail' , $data, function($msg){ 
     $msg->from('[email protected]', 'Hung Nguyen'); 
     $msg->to('[email protected]', 'Hung Manh')->subject($data['subject']); 
    }); 
    return redirect()->route('contact.index'); 
} 

當我運行顯示錯誤:

未定義變量:數據

請幫幫我。非常感謝你

回答

0

你必須將$ data傳遞給send方法中的回調函數。參考使用郵件外觀的那一行。

public function sendmail(Request $request) 
{ 
    $data = [ 
     'subject' => $request->input('subject'), 
     'name' => $request->input('name'), 
     'phone' => $request->input('phone'), 
     'email' => $request->input('email') 
    ]; 
    Mail::send('mail.sendmail' , function($msg) use($data){ 
     $msg->from('[email protected]', 'Hung Nguyen'); 
     $msg->to('[email protected]', 'Hung Manh')->subject($data['subject']); 
}); 
    return redirect()->route('contact.index'); 
}