【问题标题】:Send email to all the rows from mysql in codeigniter向codeigniter中mysql的所有行发送电子邮件
【发布时间】:2026-02-05 15:05:02
【问题描述】:

我需要向数据库表中的所有行发送电子邮件及其对应的数据。我可以通过他们的 ID 发送单个电子邮件,但现在我需要批量发送。我已经在纯 php 中完成了,但在 codeigniter 中无法弄清楚。如何做到这一点。

我的控制器是

     public function autoformcomplete()
        {
            $this->load->model('admin/Reminder_model');
            $datan = $this->Reminder_model->autoformemail();

//this should be in whileloop, that i am not able to figure out
             $email = $datan[0]['email'];
                $config = array(
                        'protocol'  => 'smtp',
                        'smtp_host' => 'email-smtp.eu-west-1.amazonaws.com',
                         'smtp_port' => 587,
                        'smtp_crypto' => 'tls',
                        'smtp_user' => 'user',
                         'smtp_pass' => 'pass',
                        'mailtype'  => 'text',
                         'charset'   => 'utf-8',
                         );
              $this->email->initialize($config);
              $this->email->set_mailtype("html");
              $this->email->set_newline("\r\n");
              $this->email->set_crlf("\r\n");
              $subject = "Complete Your Partially Filled Application-".$appid."";
              $data['datan'] = $datan;
              $mesg = $this->load->view('email/reminder/form_complete',$data,true);
              $this->email->to($email);
              $this->email->from('mail@mail.com','My Company');
              $this->email->subject($subject);
              $this->email->message($mesg);
              $this->email->send();
              echo "sent";

        }

我的模特是

public function autoformemail(){

        $this->db->order_by('id', 'DESC');
        $query = $this->db->get('appstbldata');
        return $query->result_array();

       }

【问题讨论】:

    标签: email model-view-controller while-loop codeigniter-3


    【解决方案1】:

    只需使用 foreach 循环

    public function autoformcomplete() {
        $this->load->model( 'admin/Reminder_model' );
        $datan = $this->Reminder_model->autoformemail();
    
        foreach ( $datan as $index => $val ) {
            // $val now references the current pointer to an element in your $datan array
            $email  = $val['email'];
            $config = array(
                'protocol'    => 'smtp',
                'smtp_host'   => 'email-smtp.eu-west-1.amazonaws.com',
                'smtp_port'   => 587,
                'smtp_crypto' => 'tls',
                'smtp_user'   => 'user',
                'smtp_pass'   => 'pass',
                'mailtype'    => 'text',
                'charset'     => 'utf-8',
            );
            $this->email->initialize( $config );
            $this->email->set_mailtype( "html" );
            $this->email->set_newline( "\r\n" );
            $this->email->set_crlf( "\r\n" );
            $subject       = "Complete Your Partially Filled Application-" . $appid . "";
            $data['datan'] = $datan;
            $mesg          = $this->load->view( 'email/reminder/form_complete', $data, true );
            $this->email->to( $email );
            $this->email->from( 'mail@mail.com', 'My Company' );
            $this->email->subject( $subject );
            $this->email->message( $mesg );
            $this->email->send();
            echo "sent";
        }
    
    }
    

    我不知道你的 $data['datan'] = $datan; 行是什么意思

    【讨论】:

    • 这一行我用来传递数据来查看,因为电子邮件内容在那里..当我根据应用程序ID发送电子邮件时。
    • 但是在这个while循环中,我认为它不再起作用了。
    • 你可以在这里使用一段时间,但你需要自己跟踪索引,因为你有一个数据数组不是要检查的条件; for 或 ```foreach`` 循环在这里效果更好。