php - Cakephp not redirecting after sending email through CakeEmail -


in cakephp 2.x, 1 of controller actions, trying send email user's every time new record created.

updated drmonkeyninja's suggestions:

// newsletterscontoller.php  public function add() {   if($this->request->is('post')) {     $this->newsletter->create();    if($this->newsletter->save($this->request->data)) {       $this->session->setflash(__('your newsletter has been submited.'));       $this->redirect(array('action' => 'view', $this->newsletter->id));   }   else {       $this->session->setflash(__('unable add post.'));       return $this->redirect(array('action' => 'add'));   }  } }  // newsletter.php public function aftersave($created, $options = []) {   parent::aftersave($created, $options);   if ($created === true) {     $newsletter = $this->findbyid($this->id);      // users email address.     $emails = classregistry::init('user')->find(         'list',         array(             'fields' => array(                 'user.id',                 'user.email'             ),             'conditions' => array(                 'user.email <>' => null             )         )     );      $email = new cakeemail('gmail');     $email->from(array('me@example.com' => 'caprock portal'));     $email->to($emails);     $email->subject($newsletter['newsletter']['title']);     $email->message($newsletter['newsletter']['content']);     try {        $email->send();     } catch (exception $exception) {         $this->log($exception);     }   } } 

as can see code snippet, use cakeemail send every user email containing newsletter created in action. unfortunately reason, every time cakeemail finishes sending email, cakephp ignores redirect request , proceeds render blank view (route still add). have verified cakeemail function commenting , verifying redirection starts work again. nothing caught in try-catch block either.

one of assumptions cause of problem email headers interfering redirect headers action. inspected network requests through browser, nothing seems sent out, except original post request add function.

it better send email newsletter model in aftersave() callback:-

// app/model/newsletter.php  app::uses('cakeemail', 'network/email');  public function aftersave($created, $options = []) {     parent::aftersave($created, $options);     if ($created === true) {         $newsletter = $this->findbyid($this->id);          // users email address.         $emails = classregistry::init('user')->find(             'list',             array(                 'fields' => array(                     'user.id',                     'user.email'                 ),                 'conditions' => array(                     'user.email <>' => null                 )             )         );          $email = new cakeemail('gmail');         $email->from(array('me@example.com' => 'caprock portal'));         $email->to($emails);         $email->subject($newsletter['newsletter']['title']);         $email->message($newsletter['newsletter']['content']);         try {            $email->send();         } catch (exception $exception) {             $this->log($exception);         }     } } 

your controller action be:-

// app/controller/newsletterscontroller.php public function add() {     if ($this->request->is('post')) {         $this->newsletter->create();         if ($this->newsletter->save($this->request->data)) {             return $this->redirect(array('action' => 'view', $this->newsletter->id));         } else {             $this->session->setflash(__('unable add post.'));             return $this->redirect(array('action' => 'add'));         }     } } 

if view still not rendering temporarily disable aftersave() callback check controller part of code working expected when saving newsletter.

note can filter out users without email address when retrieve users database. using find('list') don't need mess foreach loop.

you don't need use $this->newsletter->getlastinsertid(); $this->newsletter->id should have been set @ time of save.

cake's debug() method better use when debugging variables var_dump() incidentally won't work in example redirecting after has been output!


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -