Laravel 5.1中的Sendmail主题

时间:2016-03-22 07:43:34

标签: email sendmail swiftmailer subject

我希望使用变量发送带主题的电子邮件,这是代码

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('mygmail@gmail.com', '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('mygmail@gmail.com', 'Hung Nguyen');
        $msg->to('mygmail@gmail.com', 'Hung Manh')->subject($data['subject']);
    });
    return redirect()->route('contact.index');
}

当我运行显示错误时:

  

未定义的变量:数据

请帮帮我。非常感谢你

1 个答案:

答案 0 :(得分:0)

您必须将$ data传递给send方法中的回调。请参阅使用Mailfaçade的行。

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('mygmail@gmail.com', 'Hung Nguyen');
        $msg->to('mygmail@gmail.com', 'Hung Manh')->subject($data['subject']);
});
    return redirect()->route('contact.index');
}