简体   繁体   中英

How to send a batch of email with an interval of 5 mins every month using cron?

I want to perform a cronjob that has an interval of 5 minutes. So I need to send 10 emails every month until the status of all of the emails is set to 1 In my process after a successful sending of a process I am updating the sent status column in my table to 1 and also the date sent column . Here's my table structure:

+-----------+------------------+------+-----+---------+----------------+
| Field     | Type             | Null | Key | Default | Extra          |
+-----------+------------------+------+-----+---------+----------------+
| id        | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| email     | varchar(100)     | YES  |     | NULL    |                |
| sent      | tinyint(1)       | YES  |     | 0       |                |
| sent_date | date             | YES  |     | NULL    |                |
+-----------+------------------+------+-----+---------+----------------+

Here's my code for sending email:

public function sentEmailToTest() {

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

    $dummy = $this->Users_model->get_dummy_email();

    if(count($dummy) > 0) {

        $config = array(
            'protocol'  =>  'sendmail',
            'smtp_host' =>  'mail.sample.ph',
            'smtp_port' =>  587,
            'mailtype'  =>  'html', 
            'charset'   =>  'utf-8'
        );

        //what is happening every 5 minutes the loop will send the email (only 1 email per 5 minutes)
        foreach($dummy as $d) {

            $this->email->initialize($config);
            $this->email->set_mailtype("html");

            $this->email->from('info@sample.ph', 'Sample Admin');
            $this->email->to($d['email']);
            $this->email->cc('user@sample.ph, helloworld@gmail.com');
            $this->email->subject("TEST MESSAGE ONLY USING CRON");
            $this->email->message("TEST FROM SAMPLE");

            $send_mail = $this->email->send();

            if($send_mail) {
                //make update
                $this->Users_model->updateDummyStatus($d['id']);
                return true;
            } else {
                //echo $this->email->print_debugger();
            }

        }

    } else {

        //update all sent to 0 and do some validations here...

        exit;

    }

}

Here's my model function to get a batch of email with a limit of 10:

public function get_dummy_email() {

    $this->db->select('email');
    $this->db->where('sent', 0);
    $this->db->limit(10);
    $this->db->from('tr_dummy_email');
    $client = $this->db->get();

    return ($client) ? $client->result_array() : '';

}

And my cron tab is like this:

*/5 * * * *

But the problem is after running my cron. The first email did not send and it skips to the second email after 5 minutes.

What I want is send a 10 emails every 5 mins. What is happening now is only 1 email is send after 5 mins.

   $this->db->select('email');

to

     $this->db->select(array('email','id'));

because you are using id field....so maybe it causes error on update query line and stops.

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