how to send an email to a user in cakephp 2.x

1.9k Views Asked by At

I will greatly appreciate with all my heart if an expert would help me on how to send an email to a user. am building a registration system. after a user successfully applies for registration, the admin must approve and at the click of the approve button, an email is send to the user and user details are saved in the approved table. Here is the approve action in the applicationsController.

    public function approve($student_id = null) {

   if ($this->request->is('post')) 
      $application = $this->Application->findById($student_id);
       $approved['Approved'] = $application['Application'];
       $approved['Approved']['student_id'] = $approved['Approved']['student_id'];

       $status = array('Application.status' => 'approved');
       unset($application['Application']['id']);
       unset($application['Application']['receipts']);
       $this->loadModel('Approved');
       $this->Approved->create();
       if ($this->Approved->save($approved)) {
          if ($this->Approved->saveField('status', 'approved')){
          $this->Session->setFlash(__('The student has been approved'));


          $email=$this->request->data['Application']['email'];
          $this->Email->to = $email;
          $this->Email->subject = 'Registration request approval';
          $this->Email->from = '[email protected]';
          $this->Email->template = 'template';


          $this->Email->smtpOptions = array(
           'port' => '465',
           'timeout' => '30',
           'host' => 'ssl://smtp.gmail.com',
           'username' => '[email protected]',
           'password' => 'mweshaernest',
            );

          $this->Email->delivery = 'smtp';
          if($this->Email->send()){

            return true;
          }
          else{

            echo $this->Email->smtpError;
          }

       $this->Application->delete($student_id);
      $this->redirect(array('action' => 'index')); }
    } else {
       $this->Session->setFlash(__('The student could not be approved.'));
   }

   $this->set('title_for_layout', 'Approved Requests');
}

after clicking the approved button i get the following error:

Notice (8): Undefined index: Application [APP\Controller\ApplicationsController.php, line 120]

You need to specify at least one destination for to, cc or bcc. Error: An Internal Error Has Occurred.

.....bot the student gets approved and placed in the approved table

2

There are 2 best solutions below

0
On BEST ANSWER

Review u2460470's answer to point you in the right direction for generating emails with CakePHP.

Make sure you have a mail server setup to handle the processing of emails. You might already have one setup locally, something like SquirrelMail, or you may prefer to use a managed, hosted provider (like Gmail). You can find examples of configuring CakePHP to send mail through Gmail in the CakeEmail documentation.

I've had great experiences using Postmark to handle transactional emails. There is a nice plugin, maurymmarques/postmark-plugin, you can use to easily setup Postmark for your CakePHP app.

0
On
// in your controller 

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


function somrthing () {
    $Email = new CakeEmail();
    $Email->from(array('[email protected]' => 'My Site'));
    $Email->to('[email protected]');
    $Email->subject('About');
    $Email->send('My message');
}

Have a look CakeEmail in CakePHP 2.x