使用laravel中的where where条件将电子邮件发送到表中的所有记录

时间:2016-11-07 05:48:36

标签: database laravel email laravel-5.2 sendmail

我是laravel的新手。我需要使用where condition(其中exam_id = 1)向表中的所有记录发送电子邮件。每条记录都会收到电子邮件的消息是它自己的名称和电子邮件。已经存储在表中。可以为此提出任何建议吗?

提前谢谢

       public function sendmail(Request $request) {

        $email = DB::table('student')->select('email','exam_id')->where('exam_id','=','1')->get();
          $email= mysql_query("SELECT email FROM student WHERE exam_id='1' ;"); 
           $title = $request->input('title');
           $content = $request->input('content');
      if(mysql_num_rows($email))
        {
         while($elist_result = mysql_fetch_array($email))
            {
           Mail::send('email', ['title' => $title, 'content' => $content],function ($message) 
             {
             $message->from('dhivya@authorselvi.com', 'dhivya');
             $message->to('dhivya@authorselvi.com');
                $message->cc($elist_result);
            $message->subject("Hello");

             }  );
               }
          }
             return response()->json(['message' => 'message send successfully']);
              }

2 个答案:

答案 0 :(得分:1)

public function sendmail(Request $request) {

        $email = DB::table('student')->select('email','exam_id')->where('exam_id','=','1')->get();

        $title = $request['title'];
        $content = $request['content'];


        foreach($email as $email) {
           Mail::send('email', ['title' => $title, 'content' =>   $content],function ($message) 
             {
             $message->from('dhivya@authorselvi.com', 'dhivya');
             $message->to($email->email);
             $message->subject("Hello");

             });
        }

     return response()->json(['message' => 'message send successfully']);
}

答案 1 :(得分:0)

我看到你坚持使用清除PHP ,而且在使用Laravel框架时这是错误的。

public function sendmail(Request $request) {

        $emails = DB::table('student')->select('email','exam_id')->where('exam_id','=','1')->get();

        $title = $request['title'];
        $content = $request['content'];


        foreach($emails as $email) {
           Mail::send('email', ['title' => $title, 'content' =>   $content],function ($message) 
             {
             $message->from('dhivya@authorselvi.com', 'dhivya');
             $message->to($email->email);
             $message->subject("Hello");

             });
        }

     return response()->json(['message' => 'message send successfully']);
}

看看我编辑的代码并试一试。