douhuan3420 2016-02-21 18:34
浏览 23
已采纳

如何在用户控制器中输入laravel邮件中的外部变量?

I have a function in user controller in laravel that allows users to enter their name and email in a form and send an email to administrator. But when I try to the variable containing them in the MailTo function, it gives error. Here is my code:

public function send_email_contact_us(){
        $name = Input::get('name');
        $email = Input::get('email');
        $message_contact = Input::get('message');
        $sender_email = Input::get('email');
            $sender_name = Input::get('name');

        $validator = Validator::make(
            array(

                'name' => $name,
                'email' => $email

            ), array(
                'name' => 'required',
                'email' => 'required',

            )
        );

        if ($validator->fails())
        {
            $error_messages = $validator->messages()->all();
            return Redirect::back()->with('flash_errors',"Message not sent, please try again.");
        }
        else
        {
            $data=array("name"=>$name,"email"=>$email,"message_contact"=>$message_contact);
            Mail::send('emails.contactus',$data, function($message)
            {
       $message->from($sender_email, $sender_name); // THIS GIVES ERROR
        $message->to("admin@admin.com")->subject('Contact Us');
            });
            return Redirect::back()->with('flash_success',"Message sent successfully.");
        }
    }

Any help would be highly appreciated.

  • 写回答

1条回答 默认 最新

  • dsbruqxgt820011351 2016-02-21 18:39
    关注

    change to this

    Mail::send('emails.contactus',$data, function($message) use($sender_email,$sender_name)
    {
       $message->from($sender_email, $sender_name);
       $message->to("admin@admin.com")->subject('Contact Us');
    });
    

    To use external variables in a closure you have to import the variable into the closure by using the use keyword

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题