0

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

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');
}

当我运行显示错误时:

未定义变量:数据

请帮我。太感谢了

4

1 回答 1

0

您必须在发送方法中将 $data 传递给回调。请参阅使用 Mail 外观的行。

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');
}
于 2016-03-28T08:35:07.113 回答