电子邮件不发送给多个用户Cakephp

时间:2017-05-09 05:59:39

标签: php email cakephp

我想向数据库users中的所有用户发送电子邮件。我使用cakePHP,当我尝试错误时:

  

电子邮件只是发送给数组中的第一个用户

这是EmailTemplatesController.php

 public function admin_send_notification() {

    $this->loadModel("EmailTemplate");
    $this->loadModel("User");

    $adminEmailData = $this->User->find('first', array('conditions' => array('User.id' => 313)));
    $userData = $this->User->find('all', array('fields'=>array('email'), 'conditions' => array('User.role_id' => 2)));

    $check = [];    
    foreach ($userData as $value) {
        array_push($check, $value['User']);
    }

    $email = [];
    foreach ($check as $value) {
        array_push($email, $value['email']);
    }


    $adminEmailFrom = $adminEmailData['User']['email']; 

    $this->app_update_notification($email,'app-update-notification',$adminEmailFrom);

}

function app_update_notification($email, $slug, $adminEmailFrom) {  

    $this->loadModel('EmailTemplate');
    foreach ($email as $to) {

    $from           =   $adminEmailFrom;

    $mail_message   =   ''; 

    $emailTemplate  =   $this->EmailTemplate->find('first', array('conditions'=>array('EmailTemplate.slug'=>$slug)));

    $subject    =   $emailTemplate['EmailTemplate']['subject'];

    $mailMessage = str_replace(array('{NAME}','{SITE}'), array(Configure::read('Site.title')), $emailTemplate['EmailTemplate']['description']);
    $Template = 'default';        
    parent::__sendMail($to, $subject, $mailMessage, $from ,$Template);
    $this->Session->setFlash("Email has been send",'admin_flash_good');
    $this->redirect(array('controller'=>'email_templates', 'action'=>'index'));
    }
}

这是AppController.php

public function __sendMail($To, $Subject, $message, $From, $template = 'default', $smtp = 1, $attachment = array() ) {

    App::uses('CakeEmail', 'Network/Email');
    // echo $smtp;die;
    $email  =   new CakeEmail();
    $email->config();
    $email->to($To);
    $email->from($From); 
    $email->subject($Subject);
    $email->emailFormat('html');
    $email->attachments($attachment) ;
    $email->template($template);

    $email->layout  =   'default';

    if ($email->send($message)) {
        return true;
    } else {
        return false;
    }
}   

我该怎么办?请帮忙

0 个答案:

没有答案