【问题标题】:How to send a batch of email with an interval of 5 mins every month using cron?如何使用cron每月以5分钟的间隔发送一批电子邮件?
【发布时间】:2016-09-01 07:31:03
【问题描述】:

我想执行一个间隔为 5 分钟的 cronjob。所以我需要每个月发送10封邮件,直到所有邮件的状态都设置为1 在成功发送进程后的进程中,我将表中的sent 状态列更新为1date 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


    【解决方案1】:
       $this->db->select('email');
    

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

    因为您使用的是 id 字段....所以可能会导致更新查询行出错并停止。

    【讨论】:

    • 那么当函数在没有 cron 的情况下运行时,你是否获得了所需的输出
    • 我会尝试使用 setInterval 并获得 10 封电子邮件。它有效。我不知道为什么 cron 无法识别我的循环。 :(
    • 你不想每 5 分钟发送 10 封电子邮件
    • 是的,这就是我希望以 5 分钟间隔发送 10 封电子邮件的目的。在我目前只有 1 封电子邮件正在发送。
    • 是的,那么这是你的循环.....检查你从数据库中得到了什么......echo var_dump($dummy)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-21
    相关资源
    最近更新 更多