PHP "Undefined Function" even though it actually is in the same file

cakephp, email, function, php

Solution

Since `pending_email` is not a global function and is a method on the controller, you need to call it thusly:

`$this->pending_email($blog_id);`

The `$this` keyword refers to the blog posts controller class.

Problem

I'm using CakePHP on this particular application. When submitting a blog post on the site via `add()`, it should send a notification e-mail to an admin via `pending_email()`, but I'm getting the following error: `Fatal error: Call to undefined function pending_email() in .../app/controllers/blog_posts_controller.php on line 134` Here's the code: ``` function add($blog_id = null) { if (!empty($this->data)) { switch ($this->params['form']['submit']) { case 'draft': $this->data['BlogPost']['status_id'] = DRAFT; break; case 'approval': $this->data['BlogPost']['status_id'] = PENDING; break; case 'publish': $this->data['BlogPost']['status_id'] = PUBLISHED; break; } $this->data['BlogPost']['tags'] = $this->__tagCleanup($this->data['BlogPost']['tags']); $this->BlogPost->create(); if ($this->BlogPost->save($this->data)) { if ($this->data['BlogPost']['status_id'] == PUBLISHED) { $this->__tagUp($this->data['BlogPost']['blog_id'], $this->data['BlogPost']['tags']); } // Send the e-mail notating that a blog is pending pending_email($blog_id); $this->Session->setFlash(__('The blog post has been saved', true)); $this->redirect(array('action' => 'index', $this->data['BlogPost']['blog_id'])); } else { $this->Session->setFlash(__('The blog post could not be saved. Please, try again.', true)); } } // fetch blog_post context $this->BlogPost->Blog->recursive = -1; $blog = $this->BlogPost->Blog->read(array('id','title'),$blog_id); $this->set('blog', $blog); $this->data['BlogPost']['user_id'] = $this->user_id; if (!is_null($blog_id)) { $this->data['BlogPost']['blog_id'] = $blog_id; } } function pending_email($id) { if (!$id) { $this->Session->setFlash(__('Invalid blog post', true)); $this->redirect(array('action' => 'index')); } $this->BlogPost->contain(array( 'User' => array('fields' => 'id', 'full_name', 'name', 'last_name'), 'Tag' => array('fields' => 'name'), 'BlogPostComment' => array('fields' => array('created', 'content')), 'BlogPostComment.User' => array('fields' => array('name', 'last_name')), )); if(($blogPost = $this->BlogPost->read(null, $id)) == NULL){ $this->Session->setFlash(__('Invalid blog post', true)); $this->redirect(array('controller'=>'spotlights', 'action' => 'index')); } $this->set('blogPost', $this->BlogPost->read(null, $id)); $temp = $this->BlogPost->read(null, $id); $this->BlogPost->Blog->recursive = -1; //Blog information $title = $temp['BlogPost']['title']; $author = $temp['User']['full_name']; $date_created = $temp['BlogPost']['created']; // Properly format the excerpt $excerpt = preg_replace("/&#?[a-z0-9]+;/i","", $temp['BlogPost']['content']); $content = "<h2>Hello,</h2><p>$author has submitted a new blog post for review. <ul><li><b>Title:</b> $title </li><li><b>Author</b>: $author</li><li><b>Excerpt</b>: \"$excerpt\"</li><li><b>Created on:</b> $date_created</li></ul></p><p>You can log into the dashboard to approve this post.</p>"; $to = ADMIN_EMAIL; $from = SMTP_FROM; $subject = "New blog post submitted by $author"; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= 'From: ' . $from . "\r\n" . 'Reply-To: ' . $from . "\r\n" . 'X-Mailer: PHP/' . phpversion(); //Send the e-mail mail($to, $subject, $content, $headers); } ``` I cut out a few things for privacy sake, but shouldn't play into this issue. As always, any thoughts are greatly appreciated!

Original source