Page 1 of 1

Cakephp newbie here, can't get contact form to work!

Posted: Tue Sep 01, 2009 12:47 pm
by ptocheia
Hello,
I'm really new to CakePHP, and am trying to get a basic contact form set up for a website. I can get the form itself to appear, but when I try submitting the form, I get this error:

Fatal error: Call to undefined method EmailComponent::send() in /home/tastynib/public_html/cake/app/controllers/contacts_controller.php on line 16

Here's my controller file:

Code: Select all

<?php
class ContactsController extends AppController {
 
    var $name = 'Contacts';
    var $components = array('RequestHandler','Email');
    var $helpers = array('Html','Form');
    
    function add() {
        if ($this->RequestHandler->isPost()) {
            $this->Contact->set($this->data);
            if ($this->Contact->validates()) {
                //send email using the Email component
                $this->Email->to = 'moi@salamiday.com';  
                $this->Email->subject = 'Salami Day contact message from ' . $this->data['Contact']['name'];  
                $this->Email->from = $this->data['Contact']['email'];  
                $this->Email->send($this->data['Contact']['message']);
//              $this->redirect(array('action' => 'index'));
//              $this->Session->setFlash('Your message has been sent.');
            }
        }
    }
}
 
?>
Here's my model:

Code: Select all

<?php
 
class Contact extends AppModel {
    var $name = 'Contact';
    var $useTable = false; //not using the database
 
    var $_schema = array (
        'name' => array('type' => 'string', 'length' => 100),
        'email' => array('type' => 'string', 'length' => 255),
        'message' => array('type' => 'text', 'length' => 2000)
    );  
 
    var $validate = array(
        'name' => array(
            'rule' => 'notEmpty',
            'message' => 'Please enter your name'
        ),
        'email' => array(
            'rule' => 'email',
            'message' => 'Please enter a valid email address!'
        ),
        'message' => array(
            'rule' => 'notEmpty',
            'message' => 'Please enter a message'
        )
    );
}
?>
 
And here's my view:

Code: Select all

<!-- File: /app/views/contact/add.ctp -->   
 
<h2>Contact</h2>
 
<?php
 
$inputs = array('fieldset' => false, 'legend' => false);
echo $form->create('Contact');
echo $form->inputs();
echo $form->end('Send');
?>
 

I've looked all over the place for advice on this, but haven't found anything useful for me. I'm also not sure if maybe there's things I'm putting in the wrong directories just because of my lack of familiarity with Cake. In any case, I appreciate any advice anyone has to offer.

Thanks!

Re: Cakephp newbie here, can't get contact form to work!

Posted: Wed Sep 02, 2009 1:35 pm
by ptocheia
Nevermind, fixed it!

Re: Cakephp newbie here, can't get contact form to work!

Posted: Wed Sep 02, 2009 3:16 pm
by deejay
Hi ptocheia

Not that I need to know the answer or anything but it's normally good manners to give the answer to your problem. It's just that someone might find this post in the future while searching for the same problem.

Cheers

Re: Cakephp newbie here, can't get contact form to work!

Posted: Wed Sep 02, 2009 3:29 pm
by ptocheia
I supposed saying I 'fixed' it was a bit of a misnomer, actually. It's more like I tried a few random things, and then a different error appeared, and then I managed to take care of the other error, and then this error was suddenly gone. I really wish I knew the answer to this problem...especially since it started happening again, and I really have no clue why now!

Re: Cakephp newbie here, can't get contact form to work!

Posted: Wed Sep 02, 2009 3:38 pm
by Eran
Fatal error: Call to undefined method EmailComponent::send()
The error says it all - there is no send() method on the Email object in your controller. Possibly the Email member is not initialized and PHP creates a standard object on the spot to accommodate your parameters. var_dump() the email object and see what's inside. Perhaps your cakePHP version is not up to date?

Re: Cakephp newbie here, can't get contact form to work!

Posted: Thu Sep 03, 2009 3:36 am
by deejay
and then this error was suddenly gone.......and I really have no clue why now!
lol, sounds familiar ;)