Glenn Curtis
Glenn Curtis

Reputation: 659

CakeEmail Function, Issues & Problems

This is starting to (might have already) drive me mad! There does not seem to be a lot of help about how to use the inbuilt CakePHP CakeEmail.

I have found a number of posts on this site, as well as a you tube vid which is was helpful but the guy explaining was not specking in English so was a litle hard to follow him at times.

I have a view file with a basic form inside it, which ponits to my send() function inside my controller but I get nothing but errors.

Here is my current code:

UPDATE ::

The errors I am getting are,

     Undefined index: to [APP/Controller/ContactController.php, line 26]

And

     Error: Call to a member function subject() on a non-object 
     File: /var/www/app/Controller/ContactController.php    
     Line: 27

Which I just don't understand, I have called the CakeEmail right? I have copied the code right but these are the errors I get each time I submit the from.

Thanks Glenn.

Another Update ::

I have removed the code from before, I have got it to work (sort of)

//New Contact.cpt

echo $this->Form->create(array('controller' => 'contact', 'url' => '/contact/send') );
echo $this->Form->input('ContactForm.to');
echo $this->Form->input('ContactForm.subject');
echo $this->Form->input('ContactForm.message', array('type' => 'textarea') );
echo $this->Form->submit('Email Us', array('class' => 'Button'));
echo $this->Form->end();

//New Controller

public function send() {

    if ( !empty($this->request->data) ) {
        $email = new CakeEmail();
        $email->config('default');
        $email->from(array('[email protected]' => 'ME'))
              ->to('[email protected]')
              ->subject($this->request->data['ContactForm']['subject']);
        if ( $email->send($this->request->data['ContactForm']['message']) ) {
            $this->Session->setFlash(__('Email Sent...'), 'default', array('class' => 'success') );
        }

    }
    $this->render('/Contact/Contact');

} //End of function send

But Now I want to change the layout, my form will will be

//This is what I want ->

echo $this->Form->create(array('controller' => 'contact', 'url' => '/contact/send') );
echo $this->Form->input('ContactForm.name');
echo $this->Form->input('ContactForm.email');
echo $this->Form->input('ContactForm.phone');
echo $this->Form->input('ContactForm.subject');
echo $this->Form->input('ContactForm.message', array('type' => 'textarea') );
echo $this->Form->submit('Email Us', array('class' => 'Button'));
echo $this->Form->end();

I need to take name, email and phone number but when I add then to the $email in my controller all i get is this error

  error: Call to undefined method CakeEmail::name() 

//Controller Code Section That I changed

        $email->from(array('[email protected]' => 'Me'))
              ->to('[email protected]')
              ->name($this->request->data['ContactForm']['name']) <-That is the line I added!
              ->subject($this->request->data['ContactForm']['subject']);

If this is not the right way, which I am guesting its not, then how do I change the email layout to included the list of items I want. I have looked over (many times) the CakeEmail document but its not very clear and just does not give any working code of how to change from the default.

Thanks Glenn

Upvotes: 0

Views: 592

Answers (1)

dogmatic69
dogmatic69

Reputation: 7575

You have a few simple errors in your code.

This is wrong

->to($this->request->data['to'])

debug($this->request->data) to see the structure of the array

You have Undefined index: to [APP/Controller/ContactController.php, line 26] as an error but I do not see anything referencing index in your code. Either you are making this up or not pasted the correct code. I suspect its probably the same problem as the $this->request->data issue I mentioned.

Upvotes: 1

Related Questions