Quantcast

Can not send multiple email in cakephp

Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Can not send multiple email in cakephp

redvelvet
This post has NOT been accepted by the mailing list yet.
This post was updated on .
Help me. I want to send email to all user in from table users. But when i try, the error is: Email just send to user in first array, not all user.

this is EmailController.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');
        $this->loadModel('User');
       
        //$to       = implode(',', $recipients);
                foreach ($email as $to) {
                $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, $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;
    }
}  

Loading...