php代码使用forloop中的find函数发送电子邮件

时间:2016-06-22 08:52:41

标签: php cakephp

我的表格中有开始和结束日期,如下所示。

startdate = 2016-01-01

Enddate = 2016-06-20

现在我想在结束日期的1个月之前自动发送电子邮件。

一切正常......电子邮件也在发送..

但我的问题是,如果表格中有2行符合我的条件,我想向多个用户发送电子邮件,然后电子邮件发送给两个用户。

但我的代码只向单个用户发送电子邮件...并非所有用户。 我希望电子邮件逐个发送给所有用户。

public function certificateExpired()
    {
       $this->autoRender = False;
       $this->loadModel('Certificate');
       $data = $this->Certificate->find("all", array(
            'recursive' => -1, // should be used with joins     
            'fields' => array('User.*', 'Certificate.*'),
            'joins' => array(
                array(
                        'table' => 'users',
                        'alias' => 'User',
                        'type' => 'LEFT',
                        'conditions' => array('User.id = Certificate.user_id')
                    )
                ),
                'conditions' => array('Certificate.is_expirable' => 1,'Certificate.status' => 1)
        ));    
        foreach ($data as $row) {       
           $currentdate = date("Y-m-d");
           $date = $row['Certificate']['end_date'];
           $newdate = strtotime($date .' -1 months');
           $enddate = date('Y-m-d', $newdate);
           if ($currentdate >= $enddate) {
            //For sending email
            $data['email'] = $row['User']['email'];
            $data['name'] = $row['User']['name'];
            $data['document_name'] = $row['Certificate']['name'];
            $data['templateid'] = 19;  
            $send_mail = $this->EmailFunctions->certificateExpiredTemplate($data); 
             if ($send_mail) {
              $this->redirect(array('controller' => 'Dashboards', 'action' => 'shipperDashboard'));
              }     
           }
        }       
    }

1 个答案:

答案 0 :(得分:1)

foreach ($data as $row) {       
           $currentdate = date("Y-m-d");
           $date = $row['Certificate']['end_date'];
           $newdate = strtotime($date .' -1 months');
           $enddate = date('Y-m-d', $newdate);
           if ($currentdate >= $enddate) {
            //For sending email
            $data['email'] = $row['User']['email'];
            $data['name'] = $row['User']['name'];
            $data['document_name'] = $row['Certificate']['name'];
            $data['templateid'] = 19;  
            $send_mail = $this->EmailFunctions->certificateExpiredTemplate($data);    
           }
}

$this->redirect(array('controller' => 'Dashboards', 'action' => 'shipperDashboard'));

如上所述更改foreach循环。