如何在 cakephp 2.x 中向用户发送电子邮件

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

如果专家能帮助我如何向用户发送电子邮件,我将不胜感激。 正在建立一个注册系统。用户成功申请注册后,管理员必须批准,单击批准按钮后,将向用户发送一封电子邮件,并将用户详细信息保存在已批准 table 中。 这是 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 = 'ernestmwesha@gmail.com';
          $this->Email->template = 'template';


          $this->Email->smtpOptions = array(
           'port' => '465',
           'timeout' => '30',
           'host' => 'ssl://smtp.gmail.com',
           'username' => 'ernestmwesha@gmail.com',
           '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');
}

点击批准按钮后出现以下错误:

通知 (8):未定义索引:应用程序 [APP\Controller\ApplicationsController.php,第 120 行]

您需要为收件人、抄送或密件抄送指定至少一个目的地。 错误:发生内部错误。

.....bot 学生获得批准并被安置在批准的 table

// in your controller 

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


function somrthing () {
    $Email = new CakeEmail();
    $Email->from(array('me@example.com' => 'My Site'));
    $Email->to('you@example.com');
    $Email->subject('About');
    $Email->send('My message');
}

看看CakeEmail in CakePHP 2.x

查看 答案,为您指明使用 CakePHP 生成电子邮件的正确方向。

确保您设置了邮件服务器来处理电子邮件。您可能已经在本地设置了一个,类似于 CakeEmail 文档中的 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

我在使用 Postmark to handle transactional emails. There is a nice plugin, maurymmarques/postmark-plugin 方面有很好的经验,您可以使用它轻松地为您的 CakePHP 应用程序设置 Postmark。