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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ptocheia
Forum Newbie
Posts: 6
Joined: Tue Jul 29, 2008 11:28 am
Location: VA, US

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

Post 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!
ptocheia
Forum Newbie
Posts: 6
Joined: Tue Jul 29, 2008 11:28 am
Location: VA, US

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

Post by ptocheia »

Nevermind, fixed it!
User avatar
deejay
Forum Contributor
Posts: 201
Joined: Wed Jan 22, 2003 3:33 am
Location: Cornwall

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

Post 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
ptocheia
Forum Newbie
Posts: 6
Joined: Tue Jul 29, 2008 11:28 am
Location: VA, US

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

Post 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!
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

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

Post 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?
User avatar
deejay
Forum Contributor
Posts: 201
Joined: Wed Jan 22, 2003 3:33 am
Location: Cornwall

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

Post by deejay »

and then this error was suddenly gone.......and I really have no clue why now!
lol, sounds familiar ;)
Post Reply