Email Just Send to First User in Array (Not All UserUser)


#1

I want to send email to all user in database table users. but the error is:

the email just send to first user in array

this is EmailTemplatesController.php

public function admin_send_notification() {
	
	$this->loadModel("EmailTemplate");
	$this->loadModel("User");

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

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

function app_update_notification($email, $slug, $adminEmailFrom) {	
	
	$this->loadModel('EmailTemplate');
	$this->loadModel('User');		
	$recipients = array("febrianifdamanik@gmail.com", "f2dmk@yahoo.com");
	//$to      		= 	implode(',', $recipients);
	foreach ($email as $to) {
		# code...
	$from    		= 	$adminEmailFrom;
//	debug($to);die;
	$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['User']['email'], $subject, $mailMessage, $from ,$Template);
	$this->Session->setFlash("Email has been send",'admin_flash_good');
	$this->redirect(array('controller'=>'email_templates', 'action'=>'index'));
	}
}

this is 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;
    }
}

#2

$email = $this->User->find(‘all’, array(‘fields’=>array(‘email’), ‘conditions’ => array(‘User.role_id’ => 2)));

use for each after this.


#3

or use ->toArray(),

if you have many users its good ideia also check the server rules for maximum number of $recipients per mail and the maximum number of mails sent per hour


#4

I have used foreach in the code: foreach ($email as $to)


#5

Previously i have try to send email to all user in database, the website is running normally, but suddenly there are so much errors with detail below:

write failed: No space left on device
unserialize(): Error at offset 4069 of 4085 bytes
_cake_core_ cache was unable to write 'cake_dev_id' to File cache

is that caused the error?