如何使用cron每月发送一批间隔为5分钟的电子邮件?

时间:2016-09-01 07:31:03

标签: php codeigniter cron

我想要执行间隔为5分钟的cronjob。所以我需要每月发送10封电子邮件,直到所有电子邮件的状态都设置为1 在成功发送流程后的流程中,我将表格中的sent状态列更新为1以及date sent column。这是我的表格结构:

+-----------+------------------+------+-----+---------+----------------+
| 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    |                |
+-----------+------------------+------+-----+---------+----------------+

这是我发送电子邮件的代码:

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;

    }

}

这是我的模型功能,可获得一批限制为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() : '';

}

我的cron标签是这样的:

*/5 * * * *

但问题是在运行我的cron之后。第一封电子邮件没有发送,它在5分钟后跳到第二封电子邮件。

我想要的是每5分钟发送10封电子邮件。现在发生的事情是5分钟后只发送一封电子邮件。

1 个答案:

答案 0 :(得分:1)

// Configure bundling and minification for the project.
// More info at https://go.microsoft.com/fwlink/?LinkId=808241
[
  {
    "outputFileName": "wwwroot/css/site.min.css",
    // An array of relative input file paths. Globbing patterns supported
    "inputFiles": [
      "wwwroot/css/site.css"
      // Add custom css to bundle.
      "wwwroot/css/CustomCss.css"
    ]
  },
  {
    "outputFileName": "wwwroot/js/site.min.js",
    "inputFiles": [
      "wwwroot/js/site.js"
      // Add custom script to bundle.
      "wwwroot/js/CustomScript.js"
    ],
    // Optionally specify minification options
    "minify": {
      "enabled": true,
      "renameLocals": true
    },
    // Optinally generate .map file
    "sourceMap": false
  }
]

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

因为你正在使用id字段....所以它可能会导致更新查询行出错并停止。