简体   繁体   中英

Codeigniter when to use insert_batch()

I'm developing a mailing queue in Codeigniter that sends 100 messages at a time. I'm searching for the best way to do this and came across $this->db->insert_batch() . It looks useful but I can't find information on when or how to use it. Has anyone used it for mailing purposes?

$data = array(
   array(
      'title' => 'My title' ,
      'name' => 'My Name' ,
      'date' => 'My date'
   ),
   array(
      'title' => 'Another title' ,
      'name' => 'Another Name' ,
      'date' => 'Another date'
   )
);

$this->db->insert_batch('mytable', $data); 

// Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date'), ('Another title', 'Another name', 'Another date')

You can't use $this->db->insert_batch() for emailing purposes (obviously) because it's used to insert data into the database. What you can do is use CodeIgniter Email Class instead.

$this->load->library('email');

$this->email->from('your@email.tld', 'Your Name');
$this->email->to('your@email.tld'); // Send the email to yourself to see how it looks
$this->email->bcc('...'); // Pass in a comma-delimited list of email addresses or an array

$this->email->subject('Email Test');
$this->email->message('Testing the email class.');

$this->email->send();

In my opinion, this would be the best way to send an email to a lot of users using CodeIgniter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM