user2387319
user2387319

Reputation: 123

Emailing in CakePHP

I am trying use the Emailing System(CakeEmail Class) in my CakePHP application. When a new user registeres with my site, it'll send him/her an email saying that "You are saved, now follow this link to login". I added this feature, but its not sending any mail or show me whats the wrong with the code. This is the code: p

App::uses('AppController','Controller','CakeEmail','Network/Email');p
.
.
.
public function signUp(){
    if($this->request->is('post')){
       $this->User->create();
       if($this->User->save($this->request->data)){
          $Email=new CakeEmail('default');
          $Email->from(array('[email protected]'=>'My Site'))
                ->to($this->request->data['User']['username'])
                ->subject('Welcome to Beauty Class')
                ->send('My message');
          $this->Session->setFlash(__('You have been saved and an email has just been sent to you, check your mail-box and follow the given link to login'));
          $this->redirect();
       }else{
          $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
       }
    }
    $this->set('title_for_layout','Sign Up');
}

Upvotes: 0

Views: 130

Answers (1)

Jelmer
Jelmer

Reputation: 2693

Change

App::uses('AppController','Controller','CakeEmail','Network/Email');p
.
.
.
public function signUp(){ # ... the rest of your code

to

App::uses('AppController','Controller');
App::uses('CakeEmail','Network/Email');

public function signUp(){ # ... the rest of your code

Source

static App::uses(string $class, string $package)

Classes are lazily loaded in CakePHP, however before the autoloader can find your classes you need to tell App, where it can find the files. By telling App which package a class can be found in, it can properly locate the file and load it the first time a class is used.

Some examples:

App::uses('PostsController', 'Controller');
App::uses('AuthComponent', 'Controller/Component');
App::uses('MyModel', 'Model');

So basically the second param should simply match the folder path of the class file in core or app.


Ps. give Mark some credits. I didn't notice that the answer was already given in the comments.

Upvotes: 1

Related Questions