【发布时间】:2016-09-01 07:31:03
【问题描述】:
我想执行一个间隔为 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 分钟后仅发送一封电子邮件。
【问题讨论】:
标签: php codeigniter cron